Logo 
Search:

SQL Server Articles

Submit Article
Home » Articles » SQL Server » System FunctionsRSS Feeds

NULLIF Function

Posted By: Sarita Patel     Category: SQL Server     Views: 7778

This article explains about NULLIF function in sql server with examples.

NULLIF function returns null value if the two specified expressions are equivalent.

NULLIF is equivalent to a searched CASE function in which the two expressions are equal and the resulting expression is NULL.

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


Syntax of NULLIF Function :

NULLIF (expression1, expression2)

expression1 and expression2 are any valid sql server expression. It can be constant, column name, function, subquery, or any combination of arithmetic, bitwise, and string operators.  

Return type of NULLIF function is same as expression1.




Examples of NULLIF Function :

Example 1 : Use of NULLIF function in select clause 

SELECT ProductName, NULLIF(UnitsInStock,ReorderLevel) AS Stock
FROM   Products 

Output
ProductName             Stock
Rogede sild                 NULL
Spegesild                    95
Zaanse koeken            36
Chocolade                   15
Scottish Longbreads    NULL

Above example displays Null where both columns UnitsInStock and ReorderLevel values are same otherwise it displays value of UnitsInStock column.
  
Share: 

 
 
 

Didn't find what you were looking for? Find more on NULLIF Function Or get search suggestion and latest updates.

Sarita Patel
Sarita Patel author of NULLIF Function is from United States.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!