In SQL, the NULLIF function is used to replace an expression with NULL if expression matches a specified value.
NULLIF(expression1, expression2)
• Define the 'expression1/value1' to be evaluated for a match.
• Specify the 'expression2/value2' to return as null if it matches with the expression.
Here, If the expression1 equals the expression2, NULL is returned; otherwise, the expression is returned as is.
1. Let's assume we have a table named "[Employees]".
2. Let's assume you want to replace the value 'Female' in the Gender column with NULL.
3. Run below SQL statement :
SELECT *, NULLIF(Gender, 'Female') AS [Gender2] FROM [Employees];
4. In the above statement, the NULLIF function substitutes all occurrences of 'Female' within the Gender column with NULL.
Using this function in a select statement won't modify the [Employees] table directly, but it will only be reflected in the select statement's output.
Nullif function in SQL returns NULL if two expressions are equal, otherwise, returns the first expression.
Nullif value in SQL is the value to be compared with another expression to determine if it should return NULL.
The difference between NULL and Nullif in SQL is that NULL represents the absence of a value, while Nullif compares two expressions and returns NULL if they are equal.
Nullif and coalesce are SQL functions used for handling NULL values; Nullif compares two expressions and returns NULL if they are equal, while coalesce returns the first non-NULL expression from a list.