Possible Duplicate:
what is “=null” and “ IS NULL”
Is there any difference between IS NULL and =NULL
What is the difference between
where x is null
and
where x = null
and why does the latter not work?
Possible Duplicate:
what is “=null” and “ IS NULL”
Is there any difference between IS NULL and =NULL
What is the difference between
where x is null
and
where x = null
and why does the latter not work?
It's important to note, that NULL doesn't equal NULL.
NULL
is not a value, and therefore cannot be compared to another value.where x is null
checks whether x is a null value.where x = null
is checking whether x equals NULL, which will never be trueFirst is correct way of checking whether a field value is
null
while later won't work the way you expect it to becausenull
is special value which does not equal anything, so you can't use equality comparison using=
for it.So when you need to check if a field value is
null
or not, use:instead of:
i think that equality is something that can be absolutely determined. The trouble with null is that it's inherently unknown. null combined with any other value is null - unknown. Asking SQL "Is my value equal to null?" would be unknown every single time, even if the input is null. I think the implementation of IS NULL makes it clear.
In SQL, a comparison between a
null
value and any other value (including anothernull
) using a comparison operator (eg=
,!=
,<
, etc) will result in anull
, which is considered asfalse
for the purposes of a where clause (strictly speaking, it's "not true", rather than "false", but the effect is the same).The reasoning is that a
null
means "unknown", so the result of any comparison to anull
is also "unknown". So you'll get no hit on rows by codingwhere my_column = null
.SQL provides the special syntax for testing if a column is
null
, viais null
andis not null
, which is a special condition to test for anull
(or not anull
).Here's some SQL showing a variety of conditions and and their effect as per above.
returns only 1 row (as expected):
See this running on SQLFiddle