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

MySQLMySQLi Database

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)
raja
Published on 20-Feb-2018 13:56:24
Advertisements