The two constructs are not the same. From the MySQL documentation page:
NULLIF(expr1,expr2)
Returns NULL if expr1 = expr2 is true, otherwise returns expr1.
This is the same as:
CASE WHEN expr1 = expr2
THEN NULL
ELSE expr1
END.
Therefore, this logic is testing a column for "NULL
or an empty string."
- If
my_column
is presently NULL, the function will return its value (NULL ...) because it is not equal to an empty-string.
- If
my_column
is an empty string, the function will return NULL.
- If any other value, that value will be returned.
I heartily agree with Naveed's answer in saying, "this is 'cute' ... don't be 'cute'." His answer ("use OR
...") results in very simple SQL that is instantly obvious at first glance. This "clever" solution is not. (And it is probably slightly-less efficient, to boot.)
And, of course, you'd really like to avoid the necessity for this sort of thing when designing your database. You never want to store "empty strings" in a VARCHAR-type field unless it truly is the case that "we have a non-NULL value, and that value really is 'an empty string.'"