SQL SERVER 2005 – What is NULLIF ?

Returns a null value.

NULLIF returns the first expression if the two expressions are not equal. If the expressions are equal, NULLIF returns a null value of the type of the first expression.

Create Table #temp
(
id
int,
FName varchar(20),
Address varchar(100)
)

Insert into #temp values(1,‘Rana’,‘Code project address’)
go
Insert into #temp values(1,‘Roger’,‘US Florida’)
go
Insert into #temp values(1,‘Toni’,‘US’)
 

SELECT id, FName, Address,
NULLIF(Address,‘US Florida’)AS ‘Null if Equal’
FROM #temp

NULLIF is equivalent to CASE function

SELECT id, FName, Address,
CASE WHEN Address =‘US Florida’ THEN NULL ELSE Address END AS ‘Null if Equal’
FROM #temp

Result

Advertisements

One thought on “SQL SERVER 2005 – What is NULLIF ?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s