PostgreSQL – FORMAT Function
Last Updated :
17 Jul, 2024
In PostgreSQL, the FORMAT() function is a powerful tool for formatting arguments based on a specified format string.
Let us better understand the FORMAT() Function in PostgreSQL, its syntax, components, and practical usage to help you leverage this function effectively.
Syntax
FORMAT(format_string [, format_argument [, ....] ])
Let’s analyze the above syntax:
- The FORMAT() function is variadic, meaning, users can supply the arguments as an array marked with the VARIADIC keyword.
- The FORMAT() function considers the array’s elements as normal arguments and it treats NULL as an array of zero elements.
Parameters:
1. ‘format_string’
- The ‘format_string’ sets the behaviour of the resulting formatted string. It also has text and format specifiers. In this function, the text arguments are copied directly to the result string and the format specifiers are placeholders for the arguments.
Syntax of the format specifier:
%[position][flags][width]type
Note that a format specifier begins with a “%” and it has three optional components position, flags, width, and a required component type.
2. ‘position’
- It is used to set the argument that is to be inserted in the result string. The position is in the form of ‘n$’ where ‘n’ is the argument index. The first argument starts from 1. The default is the next argument in the list if the position component is not set.
3. ‘flags’
- This component is used in conjunction with the width field, for instance, the flags can accept a minus sign (-) that instructs the format specifier’s output to be left-justified.
4. ‘width’
- This optional argument sets the minimum number of characters for the format specifier’s output. If the string is shorter than the specified width, padding fills the empty spaces. If longer, the string remains unchanged.
The width argument can hold the following values:
- A positive integer value.
- An asterisk (*) to use the next function argument as the width.
- A string of the form *n$ to use the nth function argument as the width.
5. ‘type’
- It is used to define the type of the output string from the format specifier.
It can hold the following values:
- ‘s’: It formats the argument value as a string. NULL values are treated as an empty strings.
- ‘I’: It treats the argument value as an SQL identifier.
- ‘L’: It makes the argument value as an SQL literal.
'%%': Includes the % sign.
‘I‘ and ‘L‘ are generally used for constructing dynamic SQL statements.
6. ‘format_arg’
- The users pass a number of format arguments to the ‘FORMAT’() function.
Return value
- The FORMAT() function returns a formatted string.
PostgreSQL FORMAT Function Examples
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:

Example 2: Constructing Customer Full Names
The following statement uses the FORMAT() function to construct customer’s full names from first names and last names from the customers table of the sample database, ie, dvdrental:
SELECT
FORMAT('%s, %s', last_name, first_name) full_name
FROM
customer;
ORDER BY
full_name;
Output:

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.