IFNULL
replacing NULL with a default value.
Overview
MySQL IFNULL(value, fallback) uses the fallback only when the first expression is NULL; empty strings, zero, and FALSE are not replaced. Keep the fallback type compatible with the source column to avoid implicit conversion in sorting or arithmetic. COALESCE is usually preferable for portable SQL.
Basic syntax
SELECT IFNULL(phone, 'N/A') AS phone FROM customers;
Examples
IFNULL example
The example displays N/A for NULL customer phone values while preserving existing numbers; it does not modify the source table.
Sample table
customers| phone |
|---|
| 138-0000-0001 |
| NULL |
| 138-0000-0003 |
Run result
| phone |
|---|
| 138-0000-0001 |
| N/A |
| 138-0000-0003 |
Common issues
No. IFNULL uses the fallback only for NULL; an empty string, zero, and FALSE are not NULL.
MySQL IFNULL accepts two arguments. COALESCE checks multiple candidates and is more portable across databases.
The database must derive one result type. Mixing numbers and text may cause implicit conversion that affects comparisons, sorting, or application reads.
Tags
Was this explanation clear?
Thanks. This feedback will inform the next content review.