In this post explains how to use the NULLIF function in SQL Server (Transact-SQL) with syntax and examples.
In SQL Server (Transact-SQL), the NULLIF function compares expression1 and expression2. If expression1 and expression2 are equal, the NULLIF function returns NULL. Otherwise, it returns the first expression which is expression1.
The syntax for the NULLIF function in SQL Server (Transact-SQL) is:
NULLIF( expression1, expression2 )
The expressions that will be compared. Values must be of the same datatype.
The NULLIF function can be used in the following versions of SQL Server (Transact-SQL):
Let's look at some SQL Server NULLIF function examples and explore how to use the NULLIF function in SQL Server (Transact-SQL).
For example:
SELECT NULLIF('AODBA.com', 'AODBA.com');
Output: NULL (returns NULL because values are the same)
SELECT NULLIF('mySite.com', 'AODBA.com');
Output: 'mySite.com' (returns first value because values are different)
SELECT NULLIF(12, 12);
Output: NULL (returns NULL because values are the same)
SELECT NULLIF(12, 45);
Output: 12 (returns first value because values are different)
SELECT NULLIF('2014-05-01', '2014-05-01');
Output: NULL (returns NULL because values are the same)
SELECT NULLIF('2014-05-01', '2014-04-30');
Output: '2014-05-01' (returns first value because values are different)