CASE WHEN NULL does the wrong result in SQLite?

I have a table with an image type column, there are several rows in the table, but all rows do not yet have an image, all of them are zero. To test CASE WHEN NULL, I tried this and it gave a weird result:

SELECT CASE myImageColumn WHEN NULL THEN 0 ELSE 1 END FROM myTable 

All returned rows were in column 1 (I thought 0). What is wrong here?

Your help will be greatly appreciated!

Thanks!

+4
source share
3 answers

You cannot compare with NULL like this, you should try:

 SELECT CASE WHEN myImageColumn IS NULL THEN 0 ELSE 1 END FROM myTable 
+23
source

Instead, use CASE different form:

 SELECT CASE WHEN myImageColumn IS NULL THEN 0 ELSE 1 END FROM myTable 

Two useful links:

+5
source

There is a bypass here:

 CASE ifnull(myValue, 'someUniqueStringOrValue') WHEN 'someUniqueStringOrValue' THEN 0 -- this means null WHEN 'someNormalValue' THEN 1 END 
0
source

All Articles