SQL SERVER 2005 – What is NULLIF ?

Posted by Joggee | SQL Tips and Tricks | Thursday 20 September 2007 4:25 pm

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

1 Comment »

  1. Comment by dotnetolympians — September 20, 2007 @ 4:51 pm

    what do you mean by null value of the type of the first expression. Do we have different Nulls for integer and character types ?

RSS feed for comments on this post. TrackBack URI

Leave a comment