Saturday, April 3, 2010

COALESCE


In most of DB engine you can find COALESCE function. It is helpful where you need to work with NULL values.

source table


mysql> select * from INFO;
+----+-------------+--------------+
| ID | GALLERY | PHOTOGRAPHER |
+----+-------------+--------------+
| 1 | Cars | Tom |
| 2 | Countryside | Petr |
| 3 | City | Jan |
| 4 | Sport | NULL |
+----+-------------+--------------+

When you need to return not NULL report, you can use COALESCE

COALESCE function


mysql> select GALLERY, coalesce(PHOTOGRAPHER,"unknown") as PHOTOGRAPHER from coalesce;
+-------------+--------------+
| GALLERY | PHOTOGRAPHER |
+-------------+--------------+
| Cars | Tom |
| Countryside | Petr |
| City | Jan |
| Sport | unknown |
+-------------+--------------+

No comments:

Post a Comment