How can I use IFNULL() function at the place of COALESCE() function in MySQL?


As we know that IFNULL() function will return the first argument if it is not NULL otherwise it returns the second argument. On the other hand, COALESCE() function will return first non-NULL argument. Actually, both IFNULL() and COALESCE() functions in MySQL works equivalently if the number of arguments is two only. The reason behind this is that IFNULL() function accepts only two arguments and in contrast, COALESCSE() function can accept any number of arguments.

Suppose if we want to use IFNULL() function at the place of COALESCE() function then the number of arguments must be two. Following example will demonstrate it −

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

mysql> Select COALESCE(NULL, 'Green');
+-------------------------+
| COALESCE(NULL, 'Green') |
+-------------------------+
| Green                   |
+-------------------------+
1 row in set (0.00 sec)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 20-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements