Pages

Wednesday, March 11, 2015

NULLIF Function

Definition : The NULLIF function takes two arguments. If the two arguments are equal, then NULL is returned. Otherwise, the first argument is returned. The syntax for NULLIF is as follows: NULLIF ("expression 1", "expressions 2")
Real world example of NULLIF : If you want to compare column with empty string or null condition in SQL Server then generally we are write two condition with OR operator.
Sample : SELECT  COLUMN1,COLUMN2 
  FROM TABLE1 
 WHERE ( COLUMN1 IS NULL OR COLUMN1 = ‘’)

Instead of writing two condition you can use NULLIF function and write the Single condition.

Sample : SELECT  COLUMN1,COLUMN2 
  FROM TABLE1 
                 WHERE NULLIF (COLUMN1,’’) IS NULL

No comments:

Post a Comment