CASE WHEN NULL makes wrong result in SQLite?

17,176

Solution 1

You can't compare with NULL like that, you should try:

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

Solution 2

Use a different form of CASE instead:

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

Two useful links:

Solution 3

There's a bypass:

CASE ifnull(myValue, 'someUniqueStringOrValue')
  WHEN 'someUniqueStringOrValue' THEN 0 -- this means null
  WHEN 'someNormalValue' THEN 1
END
Share:
17,176

Related videos on Youtube

King King
Author by

King King

I was so lucky when reaching this reputation point :) Mainly focus on .NET (C#), program in all possible platforms using .NET technology: WPF for desktop development, ASP.NET MVC/Core for web development and Xamarin Forms for mobile development. It's true that you have to spend your whole life just to learn everything about .NET not even other fields of programming, software development and information technology. Feeling buried in the colossal world of IT. My contacts e: [email protected] Facebook LinkedIn For those who prefer the so-called FireFox (tested on v.33.0.2), just try this Flying flag effect to see how fast it is, also try that demo on Chrome, Opera and IE to see the difference. Now it's time for webkit-based browsers SUCKing. So technically all the browsers suck their own problems. Here is the test demo showing that. This time, IE 11 and FireFox 31 pass the test while all webkit-based browsers (Google Chrome 38, Opera 25, Maxthon 4.4.1.5000) suck.

Updated on July 07, 2022

Comments

  • King King
    King King almost 2 years

    I have a table with a column of image type, the table has some rows but all the rows haven't had any image yet, they are all null. To test the CASE WHEN NULL, I've tried this and it gave a strange result:

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

    All the returned rows were in a column of 1's (I thought 0's). What is wrong here?

    Your help would be highly appreciated!

    Thank you!

    • Mike Christensen
      Mike Christensen about 11 years
      Because null is unknown, nothing is equal to null.
  • King King
    King King about 11 years
    This doesn't work in MS SQL Server, do you know the solution for SQL Server? Thanks!
  • King King
    King King about 11 years
    Thank you, you are a little late after I said thanks to Lamak. But give you my +1. Thanks.
  • Doo Dah
    Doo Dah about 6 years
    Good idea. Thanks.