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
| PostgreSQL | SQLite | |
|---|---|---|
| Available | format: yes (text only). printf: no | printf and format: yes |
| Syntax | format(format, value, …) -- %s %I %L only; to_char(number, 'FM990.00') for numbers | printf(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
| name | label |
|---|---|
| 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
| code | ordered_on |
|---|---|
| RP-0001 | 2026-03-01 |
| RP-0002 | 2026-03-01 |
| RP-0003 | 2026-03-03 |
| RP-0004 | 2026-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.
| Case | PostgreSQL | SQLite |
|---|---|---|
| Two decimals | to_char(3.14159, 'FM990.00')'3.14' | printf('%.2f', 3.14159)'3.14' |
| Zero-padded to 5 digits | lpad('42', 5, '0')'00042' | printf('%05d', 42)'00042' |
| Thousands separator | to_char(1234567, 'FM9,999,999')'1,234,567' | printf('%,d', 1234567)'1,234,567' |
| Text placeholders | format('%s has %s pets', 'Ann', 3)'Ann has 3 pets' | printf('%s has %d pets', 'Ann', 3)'Ann has 3 pets' |
| A NULL value | format('[%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 printf | not available | format('%.1f%%', 45.45)'45.5%' |
Related
Common mistakes
Using
%dor%.2fwith 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
- Guide: SQL string functions
- Practice: SQL text practice: LIKE patterns and string functions
- Lesson: Text functions (Pro)
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.