FORMAT
Builds a string from a template, putting values in place of %s, %I and %L.
PostgreSQL 17.5
FORMAT(format_string[, value, ...])Parameters
- format_stringtext
- Template: %s for a value as text, %I for a name in double quotes, %L for a value in single quotes, %% for a percent sign
- valueoptional
- Values to insert, in order
Return value
A string (text)
Examples
Inserting values into a template
PostgreSQL 17.5
SELECT FORMAT('Hello, %s! You have %s new messages', 'Ann', 5) AS greeting;Name with status and alignment
PostgreSQL 17.5
SELECT FORMAT('%s (%s)', member_name, status) AS member,
FORMAT('%-10s|', status) AS padded
FROM FamilyMembers;NULL, a quoted string and a name
PostgreSQL 17.5
SELECT FORMAT('%s and %s', 'a', NULL) AS with_null,
FORMAT('%L', 'O''Neil') AS literal,
FORMAT('%I', 'Order items') AS identifier;Details
%s inserts NULL as an empty string, and %L as the word NULL. The width is set by a number after %: %-10s pads the value with spaces on the right to 10 characters.
In MySQL the FORMAT function does something else: it formats a number with digit grouping.