Home > SQL Server, T-SQL > NULLIF(,) In SQL Server

NULLIF(,) In SQL Server


NULLIF(Expression1, Expression2) is a SQL Server built-in function. It returns NULL value if the two given expressions are equal. otherwise, the output of the function is Expression1 and the data type of the return value is determined by the expression1.

Instance 1:

The output of the below given SQL queries are NULL. Because, both value of the expressions are equal.

SELECT NULLIF(‘SQL’, ‘SQL’)
SELECT NULLIF(10,10)

Instance 2:

The output of the SQL query is the value of the expression1.

SELECT NULLIF (‘SQLServerRider’, ‘SQLServer’)

Output: SQLServerRider

SELECT NULLIF(20,10)

Output: 20

Alternate method with CASE expression:

CASE expression can be used to implement the same scenario instead NULLIF() function

Instance:

DECLARE @Expression1 varchar(20) = ‘SQL Server 2012’
DECLARE @Expression2 varchar(20) = ‘SQL Server 2008 R2’
SELECT
CASE
WHEN @Expression1 = @Expression2 THEN NULL
ELSE @Expression1
END

Read further about ISNULL() function

Thanks for reading

Advertisements
  1. No comments yet.
  1. December 4, 2012 at 5:45 pm

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

%d bloggers like this: