9.08.2012

COALESCE vs NULLIF in SQL Server

Difference between COALESCE and NULLIF in SQL Server


S.No
COALESCE
NULLIF
1
This function works same as “if else” statement and it takes multiple arguments and it checks Null values for each if first argument is null then it considers the second value, if the both first and second arguments are nulls then it considers third argument.
This function works as “if” statement and it takes two arguments and compares the 2 argument, if both are same then returns “Null” else returns the first argument.
2
Syntax:
COALESCE(argument1, argument2, argument3[, argument4, argument6, argument7…])
Syntax:
NULLIF(argument1, argument2)
3
It Accepts N Parameters, It returns first Not Null Values

For Example:-
SELECT COALESCE(NULL,2,NULL,3)
Output :- 2

It Accepts 2 Parameters, if both are not equal then it will return first value.

For Example:-
SELECT NULLIF(1,2)
Output :- 1
First argument can be NULL, but both argument can not NULL value
4
First argument can be NULL, but both argument can not NULL value
First argument can not be NULL input

5
If Both argument are equal, then it will return first value.

For Example:-
SELECT COALESCE(2,2)
Output :- 2
If Both argument are equal, then it will return NULL value.

For Example:-
SELECT NULLIF(2,2)
Output :- NULL

No comments:

Post a Comment