w3resource
MYSQL Tutorial

MySQL NULLIF() function

NULLIF() function

MySQL NULLIF() returns NULL when the first is equal to the second expression, otherwise, it returns the first expression.

Syntax:

NULLIF(expression1, expression2);

Arguments

Name Description
expression1 An expression.
expression2 An expression.

MySQL Version: 5.6

Example: MySQL NULLIF() function

In the following MySQL statement since expressions are equal, it returns NULL.

Code:

SELECT NULLIF(2,2);

Sample Output:

mysql> SELECT NULLIF(2,2);
+-------------+
| NULLIF(2,2) |
+-------------+
|        NULL | 
+-------------+
1 row in set (0.03 sec)

Example : MySQL NULLIF() function with unequal arguments

In the following MySQL statement since expressions are not equal, it returns the first expression, i.e. 2.

Code:

SELECT NULLIF(2,3);

Sample Output:

 mysql> SELECT NULLIF(2,3);
+-------------+
| NULLIF(2,3) |
+-------------+
|           2 | 
+-------------+
1 row in set (0.00 sec)
 

Previous: IFNULL()
Next: MySQL String Functions ASCII