SQL Server: NULLIF Function

NULLIF function in SQL Server

This SQL Server tutorial explains how to use the NULLIF function in SQL Server (Transact-SQL) with syntax and examples.

Description

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.

Syntax

The syntax for the NULLIF function in SQL Servers (Transact-SQL) is:

NULLIF( expression1, expression2 )

Parameters or Arguments

expression1, expression2

The expressions that will be compared. Values must be of the same datatype.

Note

  • expression1 can be an expression that evaluates to NULL, but it can not be the literal NULL.

Applies To

The NULLIF function can be used in the following versions of SQL Servers (Transact-SQL):

  • SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example

Let’s look at some SQL Server NULLIF functions examples and explore how to use the NULLIF function in SQL Servers (Transact-SQL).

For example:

SELECT NULLIF('adglob.in', 'adglob.in');
Result: NULL                 (returns NULL because values are the same)

SELECT NULLIF('adglob.com', 'adglob.in');
Result: 'adglob.com'  (returns first value because values are different)

SELECT NULLIF(12, 12);
Result: NULL                 (returns NULL because values are the same)

SELECT NULLIF(12, 45);
Result: 12                   (returns first value because values are different)

SELECT NULLIF('2014-05-01', '2014-05-01');
Result: NULL                 (returns NULL because values are the same)

SELECT NULLIF('2014-05-01', '2014-04-30');
Result: '2014-05-01'         (returns first value because values are different)

Leave a Reply