NULLIF function compares the values of its parameters and returns NULL if they are equal. Otherwise, it returns the value of its first parameter.
Therefore, NULLIF performs the inverse operation of the COALESCE function.
Syntax
Parameters
If
<expression1> and <expression2> have different types, they will be promoted to their least common supertype for the purpose of comparison. The same type promotion rules as for the regular comparison operators (e.g. =) apply.
Return Types
If<expression1> and <expression2> have the same type, this will also be the return type of the NULLIF function.
If <expression1> and <expression2> have different types, the return type will be the least common supertype to which they are promoted for comparison purposes.
Examples
This example below highlights an instance whereNULL would be returned from NULLIF:
Rows: 1Execution time: 4.99ms
NULLIF returns the value of <expression1> because it is not equal to the value of <expression2>:
Rows: 1Execution time: 6.26ms
NULLIF have different types. The least common supertype of REAL and BIGINT is DOUBLE:
Rows: 1Execution time: 7.99ms