Skip to content
Related Articles

Related Articles

NULLIF() Function in SQL Server
  • Last Updated : 21 Jan, 2021

NULLIF() :

This function in SQL Server is used to check if the two specified expressions are equal or not.

Features :

  • This function is used to check if the two given expressions are equal or not.
  • This function returns NULL if the given two expressions are equal.
  • This function returns the first expression if the two given expressions are not equal.
  • This function comes under Advanced Functions.
  • This function accepts two parameters i.e, the first expression, and the second expression.

Syntax :

NULLIF(expr1, expr2)

Parameter :



This method accepts two parameters.

  • expr1, expr2 – Specified expressions that are to be compared.

Returns :

It returns NULL if the given two expressions are equal else it returns the first expression if the two given expressions are not equal.

Example-1 :

Using the NULLIF() function and getting the output.

SELECT NULLIF(11, 11);

Output :

NULL

Here, NULL is returned as both the expressions are equal.

Example-2 :



Using the NULLIF() function and getting the output.

SELECT NULLIF('ab', 'abc');

Output :

ab

Here, ‘ab’ is returned as output as the stated expressions are not equal.

Example-3 :

Using NULLIF() function and getting the output using variables.

DECLARE @exp1 VARCHAR(50);
DECLARE @exp2 VARCHAR(50);
SET @exp1 = '2021/01/08';
SET @exp2 = '2021/01/08';
SELECT NULLIF(@exp1, @exp2);

Output :

NULL

Example-4 :

Using NULLIF() function and getting the output using CAST() function.

SELECT NULLIF(11, CAST(11.65 AS int));

Output :

NULL

Application :

This function is used to test if the two stated expressions are equal or not.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :