Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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