PostgreSQL NULLIF() Function

In PostgreSQL, the NULLIF() function returns the null value if both the specified arguments are equal; otherwise returns the first argument.

Example: NULLIF()
NULLIF(<argument1>, <argument2>)

The two arguments must be of comparable types with the = operator.

The following example returns null because both the arguments are the same.

Example: NULLIF()
SELECT NULLIF(1, 1);

It returns the first argument if both the arguments are different.

Example: NULLIF()
SELECT NULLIF(1, 2);

When we give the first argument as A and the second argument is NULL, it returns A as NULL and cannot be compared to any other value.

Example: NULLIF()
SELECT NULLIF('A', NULL); 

The following returns null if any of the first_name and last_name are the same in the Employee table.

Example: NULLIF()
SELECT nullif(first_name, last_name) from Employee

Both the arguments specified in the NULLIF() function must be convertible to a common datatype otherwise it will raise an error.