FORMAT
Builds a string from a template, putting values in place of %s, %I and %L.
PostgreSQL 17.9
FORMAT(format_string[, value, ...])- [ ]optional part
- ...can be repeated
- format_string
- Template with placeholders for valuestext
- value
- Values to insert, in orderOptional
Return value
A string (text)
Examples
Inserting values into a template
PostgreSQL 17.9
SELECT FORMAT('Hello, %s! You have %s new messages', 'Ann', 5) AS greeting;Name with status and alignment
PostgreSQL 17.9
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.9
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 a value as text, %L as a string literal in single quotes, and %I as a table or column name, double-quoted only when needed, for example because of a space, capital letters or a keyword. FORMAT('%I', 'foo') returns foo, and FORMAT('%I', 'Order items') returns "Order items". %% outputs a percent sign.
%s inserts NULL as an empty string, and %L as the word NULL without quotes. 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.