Function Reference

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

Name with status and alignment

PostgreSQL 17.5
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.5
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 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.

See also