I recently realized that we need to use a special syntax
IS NULL to compare a literal to NULL.
= NULL not work here?
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.Sign up to join this community
Take a look at PSOUG's notes on NULL. As Fabricio Araujo hinted, NULL is not really a value like the number 4 or string 'bacon strips'. In fact, NULL is untyped in the SQL language, which is why you cannot validly use it in an equality comparison. You need the special
IS [NOT] NULL syntax to check if a value is NULL or not.
In SQL Server, we have an connection setting to get
=NULL to behave equally to
IS NULL. But in latest versions is not recommended anymore - it's even marked as deprecated.
The recommended is the SQL Standard way - the
IS [NOT] NULL operator.
(And I will not start an war whether 'NULL is a value or a status' here)... hehehe
Rather than justify the
is null syntax I think it is better to point out that there are no good general rules-of-thumb when dealing with
nulls and the syntax used to handle them. For example:
set val = nullrather than something like
set val to nullwhich might mirror the
is nullsyntax better
nullto say: "nulls behave like so-and-so here, so they should behave like such-and-such here
Here is an excellent essay on the subject from a postgres perspective. Briefly summed up by saying nulls are treated differently depending on the context and don't make the mistake of making any assumptions about them.
Oracle treats NULL as an unknown value. Ask yourself if this equality works.
(unknown amount in George's wallet) = (unknown amount in Harry's wallet)
or otherwise stated
NULL = NULL
The answer is clearly maybe, which is neither true nor false.
EDIT: In response to the comments I will add a little clarification to NULL as I have seen it used.
While NULL really means "not set", in the context of this question I believe the above statement is correct.
There are a number of reasons for a column to have a NULL value:
In some contexts, NULL may be handled in ways that might be considered incorrect, including: