10
I’ve always wondered why we should use IS NOT NULL
instead of <> NULL
?
For when I do as in the second case, no result is found.
Example:
SELECT * FROM tabela WHERE campo IS NOT NULL
Displays all lines except when campo
is not null.
Example:
SELECT * FROM tabela WHERE campo <> NULL
Displays nothing.
Observing: The first time I tried to make that comparison was with !=
and also did not work. Because the functionality of these operators are similar.
SQL understands that
null
cannot be compared and<>
is the comparison operator. A comparison of a value withnull
results in "unknown", hence the result is not what you expect. As what is being compared withnull
or the type expected for the result of the comparison, the bank will have a specific behaviour. The right way (ANSI SQL default) to check if a field is null isis null
(oris not null
for denial).– Caffé
I’ve seen people try to do != NULL, but with <> was the first time...
– SneepS NinjA
@Sneepsninja did not understand. Both operators (
!=
and<>
) are supported by Mysql and I believe that<>
is the ANSI SQL standard.– Caffé
I am saying that the first time in my life I tried to make this query (of the question) I did so:
SELECT * FROM teste WHERE x != NULL
; and went wrong. The question was in the sense of knowing why this comparison does not work and theNOT NULL
works– Wallace Maxters
@Caffé the equivalent operator to compare the NULL would be like this spine <=> NULL and no <> NULL da uma lida ai http://dev.mysql.com/doc/refman/5.6/en/comparison-operators.html
– SneepS NinjA
Funny that @Sneepsninja! How something can be
<=>
(smaller or equal to or greater) ?– Wallace Maxters
*opa para ser igual a NULL é <=> http://dev.mysql.com/doc/refman/5.6/en/comparison-operators.html#operator_equal-to
– SneepS NinjA
to fetch the nonnulls would have to gambiarrar same type NOT ( column <=> NULL);
– SneepS NinjA
@Sneepsninja What I didn’t understand was the fun part of trying '<>' to test if the column is "different from", since this is a common operator for this test. In any case, the
<=>
(Mysql special feature) is not the same asIS NOT NULL
.– Caffé
@Caffé when we filter like this
coluna <> null
or socoluna != null
does not give the same result ascoluna is not null
, at least not in Mysql– SneepS NinjA
@Sneepsninja Yes, it is there in my first comment on this question :-) I believe that currently
coluna <> null
does not work in any bank.– Caffé
@Caffé... rsrsrsrsrsr so the 'fun' part is that I had never seen someone try to seek 'not null' values like this
<> null
only that :D– SneepS NinjA
Related: Why NULL values are not selected?
– Marconi