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

Source: https://sqlstreak.com/sql/functions/printf · Tested on PostgreSQL 18.6 and SQLite 3.54.1; results generated 2026-11-22.


**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

- PostgreSQL: `format(format, value, …)  -- %s %I %L only;  to_char(number, 'FM990.00') for numbers` (format: yes (text only). printf: no)
- SQLite: `printf(format, value, …)  /  format(format, value, …)  -- %s %d %f %.2f %05d %,d` (printf and format: yes)

## Examples

### Prices with two decimals

PostgreSQL:

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

SQLite:

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

Result (both engines):

| name | label |
| --- | --- |
| BBQ Meteor | $15.50 |
| Four Cheese Orbit | $14.00 |
| Pepperoni Rocket | $13.50 |
| Veggie Nebula | $13.00 |
| Margherita | $11.00 |

### A sentence per row

PostgreSQL:

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

SQLite:

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

Result (both engines):

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

### Zero-padded order codes

PostgreSQL:

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

SQLite:

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

Result (both engines):

| code | ordered_on |
| --- | --- |
| RP-0001 | 2026-03-01 |
| RP-0002 | 2026-03-01 |
| RP-0003 | 2026-03-03 |
| RP-0004 | 2026-03-04 |

## Edge cases, measured on both engines

| Case | PostgreSQL | SQLite |
| --- | --- | --- |
| Two decimals | '3.14' | '3.14' |
| Zero-padded to 5 digits | '00042' | '00042' |
| Thousands separator | '1,234,567' | '1,234,567' |
| Text placeholders | 'Ann has 3 pets' | 'Ann has 3 pets' |
| A NULL value | '[]' | '[]' |
| %d in PostgreSQL's format() | error: unrecognized format() type specifier "d" | not available |
| format() as SQLite's other name for printf | not available | '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.
