Function Reference

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 values
text
value
Values to insert, in order
Optional

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;
greeting
Hello, Ann! You have 5 new messages

Name with status and alignment

PostgreSQL 17.9
SELECT FORMAT('%s (%s)', member_name, status) AS member,
	FORMAT('%-10s|', status) AS padded
FROM FamilyMembers;
memberpadded
Headley Quincey (father)father |
Flavia Quincey (mother)mother |
Andie Quincey (son)son |
Lela Quincey (daughter)daughter |
Annie Quincey (daughter)daughter |
Ernest Forrest (father)father |
Constance Forrest (mother)mother |
Wednesday Addams (daughter)daughter |

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;
with_nullliteralidentifier
a and 'O''Neil'"Order items"

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.

See also