Wednesday 8 April 2015

SQL Server ISNULL() function

ISNULL accepts only 2 parameters. The first parameter is checked for NULL value, if it is NULL then the second parameter is returned, otherwise it returns first parameter.

COALESCE accepts two or more parameters. One can apply 2 or as many parameters, but it returns only the first non NULL parameter.

ISNULL Function : The ISNULL( ) function is available in both SQL Server and MySQL. However, their uses are different:

SQL Server : In SQL Server, the ISNULL( ) function is used to replace NULL value with another value.
For example, if we have the following table,
Table Sales_Data

Store_Name
Sales
Store A
300
Store B
NULL

SELECT SUM (ISNULL(Sales,100)) FROM Sales_Data;
Returns the following result:
SUM (ISNULL(Sales,100))
        400

This is because NULL has been replaced by 100 via the ISNULL function, so the total becomes 300 + 100 = 400.

MySQL :
In MySQL, the ISNULL( ) function is used to test whether an expression is NULL. If the expression is NULL, this function returns 1. Otherwise, this function returns 0.
For example,
ISNULL(3*3) returns 0


ISNULL(3/0) returns 1

No comments:

Post a Comment