SQLite NULL Functions
NULL Functions
SQLite has three functions that help you work with NULL values:
| Function | Returns |
|---|---|
IFNULL(x, y) | x, or y if x is NULL |
COALESCE(x, y, z, ...) | The first value that is not NULL |
NULLIF(x, y) | NULL if x and y are equal, else x |
IFNULL()
Customers that have no orders get NULL as OrderID in a LEFT JOIN. This example shows the text "No orders" instead of NULL:
Example
SELECT Customers.CustomerName,
IFNULL(Orders.OrderID, 'No orders')
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;
Try it Yourself »
COALESCE()
COALESCE() can take many arguments, and returns the first one that is not NULL:
Note: IFNULL(x, y) is the same as COALESCE(x, y). COALESCE() is part of standard SQL, so it also works in other databases.
NULLIF()
NULLIF() returns NULL when the two values are equal. It is useful to avoid dividing by zero, because a division by NULL gives NULL: