NULLIF Function
Element | Description | Restrictions | Syntax |
---|---|---|---|
expr1 expr2 | Expressions that return values of a compatible data type | Cannot be a BYTE or TEXT data type | Expression |
- If their values are equal. then NULLIF returns NULL.
- If their values are not equal. then NULLIF returns expr1.
SELECT name, answer, NULLIF(answer, 'f') FROM booktab;
Here the first argument is a Boolean column expression that can have true ('t') or false ('f') values, and the second Boolean argument is always 'f" (for FALSE). For rows that have 'f' in the answer column, the value returned by the NULLIF function will be NULL (because the NULL value is returned when the arguments are equal). For rows that have 't' as the first argument, however, the value returned by NULLIF is always 't', because the two arguments cannot be equal when one is 't' and the other is 'f'; the first argument is returned when the two values are not equal.