How can we distinguish between MySQL IFNULL() and NULLIF() functions?


Actually, both MySQL IFNULL() and NULLIF() functions are having an almost same syntax as given below −

The syntax of IFNULL()

IFNULL(expression1, expression2)

The syntax of NULLIF()

NULLIF(expression1, expression2)

They can be distinguished in the way they return the first argument as result. IFNULL() function will return the first argument as a result if it is not NULL and NULLIF() function will return the first argument as a result if both the arguments are not same.

mysql> Select IFNULL('Ram','Shyam');
+-----------------------+
| IFNULL('Ram','Shyam') |
+-----------------------+
| Ram                   |
+-----------------------+
1 row in set (0.00 sec)

mysql> Select NULLIF('Ram','Shyam');
+-----------------------+
| NULLIF('Ram','Shyam') |
+-----------------------+
| Ram                   |
+-----------------------+
1 row in set (0.00 sec)

From the above result set of both the functions, it looks like they are similar but IFNULL() function returns ‘Ram’ because of it the first argument of it and it is not NULL. On the other hand NULLIF() function returns ‘Ram’ because it is the first argument and it is different from the second argument.

Updated on: 20-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements