Friday, June 1, 2012

What is ISNULL(), NULLIF() and COALESCE()

ISNULL():

ISNULL is used to replace value of expression, if it comes to NULL.

Syntax:

SELECT ISNULL(NULL, expression)

Example:

DECLARE @A VARCHAR(10)

SELECT ISNULL(@A,'No value has been assigned')


Output:No value has been assigned

NULLIF():

It Accepts 2 Parameters

NULLIF returns NULL,if both of the strings are equal else returns first string.

Syntax :

NULLIF(expression1, expression2)

e.g.

SELECT NULLIF('Welcome', 'Welcome')AS 'Null if Equal'

Output : NULL

SELECT NULLIF('Welcome', 'India')AS 'Null if not Equal'

Output : Welcome