I have an SQL query like
SELECT * FROM tableName WHERE colName != 'contents'
now contrary to what I expected this does NOT seem to match a colName where its contents is 'NULL'. Looking at how SQLite handles NULLs and specifially this condition "null OR true" is true
being true for SqlLite I wouldve thought my query wouldve been sufficent to select the rows with NULL
for colName
. I mustve understood this incorrectly. Can anyone shed some light on this for me? I can just use
SELECT * FROM tableName WHERE (colName != 'contents' OR colName IS NULL)
but i didnt think i would have to.
Thanks
You can get around this issue by using
IS NOT
rather than!=
. If you look at the operators section of this document you'll see:I tested this out myself:
From a bit more playing about, it seems that the
!=
operator will evaluate toNULL
when you use it to compare something withNULL
:Presumably this is why you're not getting the behaviour you expect - you are effectively saying
WHERE NULL != 'contents'
which evaluates to NULL, and doesn't satisfy the WHERE clause