28

I have a shape layer with an attribute containing NULL values to which I would like to apply a filter for values different from NULL.

enter image description here

Using the available GUI for such query construction, one would intuitively try

"obj_art" != NULL

enter image description here

Which means 'give me all the features with attribute "obj_art" different from NULL' (these count definitely more than 0). Testing this query delivers a strange result from my point of view:

enter image description here

So what I've learned so far is that I can achieve this by using

"obj_art" IS NOT NULL

The question is, what is the difference between != NULL and IS NOT NULL?

2
  • "one would intuitively try" Really? Why? NULL is not a value. It is literally the absence of a value. You can't "equal" NULL! The operator != does not mean "is not"; it means "is not equal to". IS NOT means "is not" so I think this is perfectly intuitive :) Commented Jul 26, 2016 at 9:05
  • @LightnessRacesinOrbit Without the understanding that NULL is not a value I would say it is intuitive to try to use = or != as that is how you assess any other value. It's not until you know that NULL isn't a value that it's really intuitive to use IS NOT rather than !=. Many are not aware of what a NULL truly is.
    – Midavalo
    Commented Jul 26, 2016 at 9:23

2 Answers 2

35

Disclaimer: Because the syntax for filtering in QGIS works with SQL, I'm assuming here that SQL rules apply. I'm not completely sure if that's entirely correct, but it seems logical and it does explain the behavior.


The filter works with SQL, that's why you have to look there for an answer.

In short, when using a logical operator in combination with null, the result is always null. But in order to test for null, SQL comes with the IS (NOT) comparison functionality, which allows to use it for your intended filtering.

Check @Bohemian's answer on StackOverflow for a more in-depth discussion.

1
  • 1
    Your assumption in the disclaimer is completely correct Commented Nov 23, 2021 at 7:23
24

NULL is not a value, therefore it cannot equal = or not equal != anything. It is not the same as zero 0 which is a value.

A NULL indicates that no value has been recorded in the cell you are looking at. To check if a value exists you ask if the cell IS NULL or if it IS NOT NULL

  • IS NULL checks to see if the cell is empty
  • IS NOT NULL checks to see if the cell is not empty

If you have some records where a values are One, Two, Three and the rest NULL and you want to find everything that isn't Two you would need to use something like

value != 'Two' OR value IS NULL 

as NULL values do not get returned in an equals/not equals query. If you used just value != 'Three' the result would exclude all NULL records as NULL isn't a value that can equal or not equal.

1
  • 1
    Thanks for this! Stumbled on this after a lot of search on why my A != 'yes' wasn't working. Since all other values were NULL it was returning 0 results - which was not intuitive. Commented Sep 10, 2021 at 17:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.