sqlstreak

SQL function

SQLite printf() and PostgreSQL format(): build formatted text

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Short answer

SQLite's printf(format, values…) (also called format()) fills placeholders C-style: printf('%.2f', 3.14159) is '3.14'. PostgreSQL's format() only knows %s, %I and %L; for numbers it uses to_char(3.14159, 'FM990.00').

Syntax and availability

PostgreSQLSQLite
Availableformat: yes (text only). printf: noprintf and format: yes
Syntaxformat(format, value, …) -- %s %I %L only; to_char(number, 'FM990.00') for numbersprintf(format, value, …) / format(format, value, …) -- %s %d %f %.2f %05d %,d

Examples

Prices with two decimals

PostgreSQL

SELECT name, '$' || to_char(price, 'FM990.00') AS label
FROM menu_items
ORDER BY price DESC
LIMIT 5;

SQLite

SELECT name, printf('$%.2f', price) AS label
FROM menu_items
ORDER BY price DESC
LIMIT 5;

Result (PostgreSQL and SQLite, identical): 5 rows

namelabel
BBQ Meteor$15.50
Four Cheese Orbit$14.00
Pepperoni Rocket$13.50
Veggie Nebula$13.00
Margherita$11.00

Try it: PostgreSQL playground · SQLite playground

A sentence per row

PostgreSQL

SELECT format('%s is %s years old', name, age) AS line
FROM pets
ORDER BY id
LIMIT 4;

SQLite

SELECT printf('%s is %d years old', name, age) AS line
FROM pets
ORDER BY id
LIMIT 4;

Result (PostgreSQL and SQLite, identical): 4 rows

line
Biscuit is 3 years old
Mochi is 2 years old
Pickles is 7 years old
Luna is 5 years old

Try it: PostgreSQL playground · SQLite playground

Zero-padded order codes

PostgreSQL

SELECT 'RP-' || lpad(id::text, 4, '0') AS code, ordered_on
FROM orders
ORDER BY id
LIMIT 4;

SQLite

SELECT printf('RP-%04d', id) AS code, ordered_on
FROM orders
ORDER BY id
LIMIT 4;

Result (PostgreSQL and SQLite, identical): 4 rows

codeordered_on
RP-00012026-03-01
RP-00022026-03-01
RP-00032026-03-03
RP-00042026-03-04

Try it: PostgreSQL playground · SQLite playground

How it behaves: edge cases, measured

Each expression was run as SELECT … on PostgreSQL 18.6 and SQLite 3.54.1 when this page was built. Nothing here is typed by hand.

CasePostgreSQLSQLite
Two decimalsto_char(3.14159, 'FM990.00')'3.14'printf('%.2f', 3.14159)'3.14'
Zero-padded to 5 digitslpad('42', 5, '0')'00042'printf('%05d', 42)'00042'
Thousands separatorto_char(1234567, 'FM9,999,999')'1,234,567'printf('%,d', 1234567)'1,234,567'
Text placeholdersformat('%s has %s pets', 'Ann', 3)'Ann has 3 pets'printf('%s has %d pets', 'Ann', 3)'Ann has 3 pets'
A NULL valueformat('[%s]', NULL)'[]'printf('[%s]', NULL)'[]'
%d in PostgreSQL's format()format('%d', 42)error: unrecognized format() type specifier "d"not available
format() as SQLite's other name for printfnot availableformat('%.1f%%', 45.45)'45.5%'

Common mistakes

  • Using %d or %.2f with PostgreSQL's format(): it only understands %s, %I and %L (the matrix shows the error).

  • Formatting numbers you still need to sort: the result is text, so '10.00' sorts before '9.00'. Format in the final SELECT only.

  • Rounding twice: printf('%.2f', ROUND(x, 2)) is fine; ROUND(printf(…)) turns text back into a number.

PostgreSQL and SQLite

The functions share a name (format) but not a feature set: SQLite's printf/format takes C-style number formats; PostgreSQL's format handles text and identifiers, and numbers go through to_char or lpad.

Official documentation

Go deeper

Make it stick in five minutes a day

“Text functions” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.

Start free