I recently realized that we need to use a special syntax IS NULL
to compare a literal to NULL.
Why does = 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 communityTake 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.
null
is "untyped". Consider the different result types in the following: select decode('A','B',to_char(null),'A','1') from dual;
and select decode('A','B',to_number(null),'A','1') from dual;
Aug 4, 2011 at 15:38
to_number(null)
is still null
(select * from dual where to_number(null) is null;
), it is just a 'typed' null
.
Aug 4, 2011 at 21:18
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 null
s and the syntax used to handle them. For example:
set val = null
rather than something like set val to null
which might mirror the is null
syntax betternull
to say: "nulls behave like so-and-so here, so they should behave like such-and-such hereHere 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.
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:
null
differently according to the context.
Aug 4, 2011 at 15:47
unknown
is not used at all in Oracle (unlike postgres for example)
Aug 4, 2011 at 15:49