Chris Johnson troubleshoots an issue in code:
The poster had a
CASE
statement and was wondering why it didn’t work as expected. Essentially they were doing something like:
CASE WHEN @a = @b OR (@a IS NULL AND @b IS NULL) THEN 1 ELSE 0
CASE WHEN NOT(@a = @b OR (@a IS NULL AND @b IS NULL)) THEN 1 ELSE 0
And they wanted to know why both were returning 0 when
@a
or@b
were set toNULL
. The issue here is that any normal predicate involvingNULL
returns an unknown. They had tried to compensate with theOR
, which got them the result they wanted in the first statement, but didn’t understand why it did that.
Click through for the explanation.