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: 8.0

Example: MySQL NULLIF() function

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

Code:

SELECT NULLIF(2,2);

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);

Output:

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

Previous: IFNULL()
Next: MySQL String Functions ASCII



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/mysql/control-flow-functions/null-if-function.php