PostgreSQL NULLIF() Function
In PostgreSQL, the NULLIF()
function returns the null value if both the specified arguments are equal; otherwise returns the first argument.
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.
SELECT NULLIF(1, 1);
It returns the first argument if both the arguments are different.
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.
SELECT NULLIF('A', NULL);
The following returns null if any of the first_name
and last_name
are the same in the Employee table.
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.