mysqldude

IFNULL(), NVL(), ISNULL(), NULLIF() and COALESCE()

Version compatibility

StepMySQLMariaDB
5.65.78.08.4910.610.1111.411.8
COALESCE(name, lastname) (original)
original post
✓✓✓✓✓✓✓✓✓
COALESCE(name, lastname, deptname) (original)
original post
✓✓✓✓✓✓✓✓✓
IFNULL() and NULLIF()
original post
✓✓✓✓✓✓✓✓✓
NVL() (Oracle name)
version check
✗ 1305✗ 1305✗ 1305✗ 1305✗ 1305✓✓✓✓
ISNULL(a, b) as in SQL Server (MySQL's ISNULL takes one argument)
version check
✓ 1582✓ 1582✓ 1582✓ 1582✓ 1582✓ 1582✓ 1582✓ 1582✓ 1582
COALESCE(name, lastname) (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
COALESCE(name, lastname, deptname) (original) original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
IFNULL() and NULLIF() original post
MySQL 5.6✓5.7✓8.0✓8.4✓9✓
MariaDB 10.6✓10.11✓11.4✓11.8✓
NVL() (Oracle name) version check
MySQL 5.6✗ 13055.7✗ 13058.0✗ 13058.4✗ 13059✗ 1305
MariaDB 10.6✓10.11✓11.4✓11.8✓
ISNULL(a, b) as in SQL Server (MySQL's ISNULL takes one argument) version check
MySQL 5.6✓ 15825.7✓ 15828.0✓ 15828.4✓ 15829✓ 1582
MariaDB 10.6✓ 158210.11✓ 158211.4✓ 158211.8✓ 1582
✓works ✓ 1062fails with the error the step demonstrates ⚠works with a warning ≠runs, result differs ✗fails Tested 2026-11-25 on 5.6.51, 5.7.44, 8.0.46, 8.4.13, 9.7.4, 10.6.28, 10.11.19, 11.4.13, 11.8.9
What the warnings and failures say (1)
  • NVL() (Oracle name) (MySQL 5.6, MySQL 5.7, MySQL 8.0, MySQL 8.4, MySQL 9): error 1305: FUNCTION t_3.NVL does not exist

Related errors: 1582

The original article

First published 2011-04-07. The text below is the original; the table above shows how it behaves on current versions.

MySQL offers two methods for determining a NULL value and replacing it with another. In SQL Server the function IFNULL is available and in Oracle NVL. However, standard standard SQL shows us that the COALESCE function is standard ANSI SQL and would standardize your SQL code.

The syntax for COALESCE is:

COALESCE (field, field [, field] ...)

The coalesce scans through each of the fields until the first non-null is encountered and returned. If no non-null fields are encountered then a NULL is returned.

mysql> select * from testtable;
+----+------+----------+----------+
| id | name | lastname | deptname |
+----+------+----------+----------+
|  1 | NULL | NULL     | Accounts |
|  2 | NULL | Kelly    | NULL     |
|  3 | Tom  | NULL     | NULL     |
+----+------+----------+----------+
3 rows in set (0.00 sec)

mysql> select coalesce(name, lastname) from testtable;
+--------------------------+
| coalesce(name, lastname) |
+--------------------------+
| NULL                     |
| Kelly                    |
| Tom                      |
+--------------------------+
3 rows in set (0.00 sec)

mysql> select coalesce(name, lastname, deptname) from testtable;
+------------------------------------+
| coalesce(name, lastname, deptname) |
+------------------------------------+
| Accounts                           |
| Kelly                              |
| Tom                                |
+------------------------------------+
3 rows in set (0.00 sec)

Here is a list of the native SQL calls for each of the databases and those that support COALESCE.

Database Proprietary Function Coalesce support
Oracle NVL() Yes
MySQL IF() Yes
PostgreSQL NULLIF() Yes
Sybase IFNULL() Yes
MS SQL Server ISNULL() Yes
DB2 IFNULL(),NVL() Yes
SQLite IFNULL() Yes