NULLIF
NULLIF is a function in SQL that compares two expressions. If the two expressions are equal, the function returns a NULL value. If the two expressions are not equal, the function returns the first expression.
NULLIF(expr1, expr2)
- expr1: This is the first expression to be compared in the NULLIF function. It can be a column name, a constant, or the result of another function.
- expr2: This is the second expression to be compared in the NULLIF function. Like expr1, it can also be a column name, a constant, or the result of another function.
Example
Output
Explanation
In the example, the function NULLIF
compares two expressions. When the expressions are equal, the function returns NULL
. In the first case, 4
equals 4
, so NULL
is returned. In the second case, 5
does not equal 4
, so 5
is returned.
NULLIF(expression1, expression2)
- expression1: This is the first expression to be compared. It can be any data type that is comparable.
- expression2: This is the second expression against which the first expression is compared. It should be of a comparable data type to the first expression.
Example
Output
Explanation
Output
Explanation
The NULLIF function in SQL Server compares two expressions. If they are equal, it returns NULL. If they aren’t equal, it returns the first expression. In the example, ‘SQL’ and ‘SQL’ are equal, so Test1 returns NULL. ‘SQL’ and ‘Python’ are not equal, so Test2 returns ‘SQL’.
NULLIF(expr1, expr2)
- expr1: The first expression that NULLIF() will evaluate. It can be a constant, variable, or column of a table. This expression can be of any data type.
- expr2: The second expression that NULLIF() compares against the first one. Same as expr1, this can be a constant, variable, or column and can be of any data type. If expr1 equals expr2, NULLIF() returns a NULL value.
Example
Output
Explanation
The NULLIF function in Oracle compares two expressions. If they are equal, NULLIF returns a NULL value. In this specific example, the two strings ‘Apple’ and ‘Apple’ are compared. Since they are equal, the function returns NULL.
NULLIF(expr1, expr2)
- expr1: The first expression that SQL compares in the NULLIF function. This can be any SQL valid expression - a constant, a column of a table, a function, or an algebraic operation between constants and column values.
- expr2: The second expression with which the first one is compared. Like expr1, it can also be any valid SQL expression. If the evaluated result of expr1 and expr2 are equal, NULLIF will return a NULL value. If not, it will return the result of expr1.
Example
Output
Explanation
In this example, the NULLIF
function compares two values. If the two values are the same, it returns NULL
. If they are different, it returns the first value. Thus, NULLIF('A', 'A')
returns NULL
and NULLIF('A', 'B')
returns A
.