SQL function
NULLIF() in SQL: turn a value into NULL
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
NULLIF(a, b) returns NULL when a equals b, and a otherwise. Its two everyday uses: NULLIF(phone, '') turns blanks into real NULLs, and x / NULLIF(y, 0) gives NULL instead of a division-by-zero error.
Syntax and availability
| PostgreSQL | SQLite | |
|---|---|---|
| Available | Yes | Yes |
| Syntax | NULLIF(value, value_to_treat_as_null) | NULLIF(value, value_to_treat_as_null) |
Examples
Blank phone numbers become NULL
SELECT id, phone AS raw, NULLIF(TRIM(phone), '') AS phone
FROM adoption_requests
ORDER BY id
LIMIT 6;Result (PostgreSQL and SQLite, identical): 6 rows
| id | raw | phone |
|---|---|---|
| 1 | 555-0101 | 555-0101 |
| 2 | NULL | |
| 3 | NULL | NULL |
| 4 | 555-0104 | 555-0104 |
| 5 | 555-0105 | 555-0105 |
| 6 | NULL |
Try it: PostgreSQL playground · SQLite playground
Then COUNT can skip them
SELECT COUNT(phone) AS counted_raw,
COUNT(NULLIF(TRIM(phone), '')) AS real_phones
FROM adoption_requests;Result (PostgreSQL and SQLite, identical): 1 row
| counted_raw | real_phones |
|---|---|
| 10 | 7 |
Try it: PostgreSQL playground · SQLite playground
Safe division
SELECT source,
COUNT(*) AS users,
ROUND(1.0 * SUM(CASE WHEN u.id IN (SELECT user_id FROM subscriptions) THEN 1 ELSE 0 END)
/ NULLIF(COUNT(*), 0), 3) AS conversion
FROM users u
GROUP BY source
ORDER BY source;Result (PostgreSQL and SQLite, identical): 4 rows
| source | users | conversion |
|---|---|---|
| ad | 57 | 0.316 |
| friend | 91 | 0.429 |
| podcast | 55 | 0.255 |
| search | 97 | 0.299 |
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 |
|---|---|---|
| Equal: NULL | NULLIF(5, 5)NULL | NULLIF(5, 5)NULL |
| Not equal: the first value | NULLIF(5, 0)5 | NULLIF(5, 0)5 |
| Blank text | NULLIF('', '')NULL | NULLIF('', '')NULL |
| Dividing by NULLIF(0, 0) | 10 / NULLIF(0, 0)NULL | 10 / NULLIF(0, 0)NULL |
| Case matters | NULLIF('a', 'A')'a' | NULLIF('a', 'A')'a' |
| NULL first | NULLIF(NULL, 1)NULL | NULLIF(NULL, 1)NULL |
| Number vs text | NULLIF(1, '1')NULL | NULLIF(1, '1')1 |
Related
Common mistakes
Swapping the arguments:
NULLIF('', phone)returns '' or NULL, never the phone.Forgetting TRIM:
NULLIF(phone, '')leaves' 'alone.Expecting NULLIF to replace NULLs: that's COALESCE's job; the two are often used together.
PostgreSQL and SQLite
Standard and the same in both for same-type values. With a number and text, SQLite compares them as different types and keeps the number, while PostgreSQL first converts the text to a number (see the matrix).
Official documentation
Go deeper
- Guide: Data cleaning in SQL
- Practice: SQL data cleaning practice: 6 exercises with answers
- Lesson: Empty, NULL and defaults (Pro)
Make it stick in five minutes a day
“Empty, NULL and defaults” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.