Search This Blog

Wednesday, January 12, 2011

IFNULL

The IFNULL command is not used in Oracle or SQL Server. It performs the same function of the ISNULL implementation in MS SQL Server but is used in mySQL.
This is where the differences between the various SQL database vendors becomes apparent. Rather than sticking to the standard, they all have their own variations.
In short, for mySQL servers, ISNULL will replace NULL values with a user specified value.
Say we have a table called orders like this:
---------------------
|order_id |quantity |
---------------------
|1        |10       |
---------------------
|2        |10       |
---------------------
|3        |10       |
---------------------
|4        |NULL     |
---------------------
Now, we will look at how to change NULL in mySQL.
SELECT SUM(IFNULL(quantity,5)) AS result FROM orders;
--------
|result|
--------
|35    |
--------
As you can see, we have replaced the NULL values with 5.

No comments:

Post a Comment