SQLite 3 reference · Conditional and Type Functions
SQLite IFNULL() Function
Return the first argument if it isn't NULL, otherwise the second. A two-argument coalesce().
SQLite 3
Syntax
ifnull(X, Y)
Parameters
| Parameter | Description |
|---|---|
X |
The value to check. |
Y |
The fallback. |
Return value
X if it isn't NULL, otherwise Y.
Examples
SELECT name, ifnull(city, 'unknown') AS city
FROM customers
WHERE customer_id IN (6, 7);
Output
name | city Tim Berners-Lee | Geneva Barbara Liskov | unknown
SELECT p.name, ifnull(c.name, 'Uncategorised') AS category
FROM products AS p
LEFT JOIN categories AS c USING (category_id)
WHERE p.product_id IN (11, 12);
Output
name | category Rubber Duck | Toys Robot Kit | Uncategorised
In other languages
COALESCE()· PostgreSQL
Related
COALESCE()· Return the first argument that isn't NULL. The standard way to replace NULL with a default value.NULLIF()· Return NULL if two values are equal, otherwise the first value. Often used to avoid division by zero.- SQL NULL Values · tutorial