So, I asked a question this morning, which I did not phrase correctly, so I got a lot of responses as to why NULL compared to anything will give NULL/FALSE.
My actual q
The trouble is that a<>b
(or a=b
) yields NULL
, not 1
or 0
when one or both operands are NULL. This doesn't matter for the =
case because NULL OR 1
is 1
and NULL OR 0
is NULL
which behaves like 0
for selecting in a WHERE
clause.
You could say:
a<>b OR (a IS NULL)<>(b IS NULL)
However needing to do it either way may be a sign that you're misusing NULL and should consider changing the schema to use some other NOT NULL value to signify this comparable condition.
For example if you've got a person
table with a title
column, don't use NULL to signify that they have no title; that's not a ‘missing’ datum, it's just that no title exists. So store it as an empty string ''
that you can happily compare with other empty strings. (Well unless you run Oracle of course, with its Empty String Problem...)
If
where ((A=B) OR (A IS NULL AND B IS NULL))
is for equality, then why just not use:
where NOT (
((A=B) OR (A IS NULL AND B IS NULL))
)
for inequality?