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