Full answer
sql
SELECT CASE WHEN NULL THEN 'yes' ELSE 'no' END AS searched, CASE NULL WHEN NULL THEN 'match' ELSE 'no match' END AS simple, CASE WHEN NULL IS NULL THEN 'match' END AS with_is;| searched | simple | with_is |
|---|---|---|
| no | no match | match |
In the searched form, each WHEN condition is evaluated and only a true result selects the branch; unknown behaves like false. The simple form CASE x WHEN y is shorthand for CASE WHEN x = y, so a NULL on either side gives unknown. To branch on NULL, use the searched form with IS NULL, as the third column does, returning 'match'. This also explains why CASE status WHEN NULL THEN ... silently never fires in real queries.
sql
SELECT CASE WHEN NULL THEN 'yes' ELSE 'no' END AS searched, CASE NULL WHEN NULL THEN 'match' ELSE 'no match' END AS simple, CASE WHEN NULL IS NULL THEN 'match' END AS with_is;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- CASE branches need true, not unknown
- Simple CASE uses = comparison
- Use WHEN x IS NULL to test NULL
Common mistakes
- Writing CASE col WHEN NULL THEN ...
Follow-ups
- How would you label NULL statuses as 'unknown'?
- Does COALESCE help here?
Tips
- Translate simple CASE into =