The Wayback Machine - https://web.archive.org/web/20250224055232/https://www.geeksforgeeks.org/postgresql-format-function/
Open In App

PostgreSQL – FORMAT Function

Last Updated : 14 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The PostgreSQL format() function is a powerful tool for string formatting by allowing developers to insert variables into strings using format specifiers like %s, %I, and %L. This function is especially useful for building dynamic SQL queries and ensuring proper formatting of identifiers.

It simplifies complex string manipulations and enhances security by preventing SQL injection. In this article, We will learn about the FORMAT Function in PostgreSQL in detail by understanding various aspects.

PostgreSQL Format() Function

  • The format() function in PostgreSQL is used to format strings in a way similar to the printf() function in C.
  • It allows placeholders (like %s, %I, or %L) to be replaced with variable values.
  • This can be particularly useful in dynamically generating SQL statements, user-friendly messages or customized data outputs in queries.

Syntax

format(format_string [, format_arg [, ... ]])

Explanation:

  • format_string: The string containing the format specifiers.
  • format_arg: The arguments to replace the format specifiers.

Supported Format Specifiers

The format() function supports various format specifiers to handle different data types:

Specifier Description
%s Replaces with a string.
%I Replaces with an identifier (table or column name).
%L Replaces with a literal (safely quoted).
%t Replaces with a Boolean.
%D Replaces with a numeric, double-precision number.

Examples of Using the format() Function

Let us take a look at some of the examples of FORMAT Function in PostgreSQL to better understand the concept.

Example 1: Simple String Formatting

The following statement uses the FORMAT() function to format a string:

SELECT FORMAT('Hello, %s', 'Geeks!!');

Output:

PostgreSQL FORMAT Function Example

Example 2: Constructing Customer Full Names

The following statement uses the FORMAT() function to construct customer’ full names from first names and last names from the customers table of the sample database which is dvdrental:

SELECT 
FORMAT('%s, %s', last_name, first_name) full_name
FROM
customer;
ORDER BY
full_name;

Output:

PostgreSQL FORMAT Function Example

Important Points About FORMAT Function in PostgreSQL

  • The FORMAT() function is variadic, meaning it can accept a varying number of arguments.
  • The ‘I' and ‘L' type specifiers allow the FORMAT() function to format SQL identifiers and literals.
  • The ‘-‘ flag left-justifies the output, while the ‘0' flag pads the output with zeros instead of spaces.
  • The FORMAT() function allows the use of positional parameters with the syntax ‘%n$', where n is the position of the argument in the list.

Conclusion

The PostgreSQL format() function is a versatile tool for formatting strings, making it an excellent choice for dynamic SQL generation and improving code readability. Whether you’re dealing with simple text or complex identifiers and literals, the format() function provides a robust way to customize your outputs and queries.

FAQs

How to Format Data in PostgreSQL?

In PostgreSQL, you can format data using the `format()` function, which allows you to insert variables into a string with placeholders like `%s` for strings, `%I` for identifiers, and `%L` for literals. This is useful for constructing dynamic SQL queries or formatting output.

What is the Function Format String in PostgreSQL?

The function format string in PostgreSQL refers to the `format()` function, which uses format specifiers like `%s`, `%I`, and `%L` to insert variables into a string. It helps format dynamic SQL queries or custom text outputs with proper quoting and formatting.



Next Article

Similar Reads

three90RightbarBannerImg