SQL ISNULL

The SQL ISNULL function has different meanings in different databases. In MS SQL Server, ISNULL is used to replace NULL value, while in MySQL, ISNULL is used to tell if an expression is NULL or not.

SQL Server

In SQL Server, the SQL ISNULL function is used to replace NULL value with an alternate value. It’s same with the SQL NVL function in Oracle or SQL IFNULL function in mySQL.

SQL ISNULL Function in MS SQL Server Syntax

ISNULL(exp1, exp2)

If exp1 is null, the ISNULL function will return exp2, otherwise it will return exp1.

SQL ISNULL Function in MS SQL Server Example

Table: Employees

ID EmpName
001 Juiaaa
002 Hiooi
003 NULL
004 Qiak
SELECT ID, ISNULL(EmpName, 'N/A') AS EmpName
FROM Employees

The result will look like:

ID EmpName
001 Juiaaa
002 Hiooi
003 N/A
004 Qiak

MySQL

In MySQL, the SQL ISNULL function is used to tell if an expression is NULL or not.

SQL ISNULL Function in MySQL Syntax

ISNULL(exp1)

If exp1 is null, the ISNULL function will return 1, otherwise it will return 0.

SQL ISNULL Function in MySQL Example

Table: Employees

ID EmpName
001 Juiaaa
002 Hiooi
003 NULL
004 Qiak
SELECT ID, ISNULL(EmpName) AS NoName
FROM Employees

The result will look like:

ID NoName
001 0
002 0
003 1
004 0

Leave a Comment