I'm trying to write an expression which will return a TRUE or 1 type result if the string in "column3" is the same as the string in either "column2" or "column1".

I've tried "column3" IN ("column2","column1") which would seem logically to do the job, but it just returns a blank cell.

I'm using QGIS 2.6.

  • 1
    Strange, I've tested your expression which should work. Try the following which makes sure that the values are converted to string: tostring("column3") IN (tostring("column2"), tostring("column1")). – Joseph Oct 5 '15 at 15:23
  • 1
    Can you clarify (a) what you mean by "a blank cell", and (b) where you are trying to use this expression. I just tested in the labelling engine in 2.8, and expression evaluates successfully so that all features get labelled with 0 or 1. – Lee Hachadoorian Oct 6 '15 at 1:51
  • I've worked out what the problem was, it's that fact that some entries in "column 3" are null and this seems to make the whole expression fail. I would have thought that logically 'NULL' does not equal '(any other string)' and so should return a 0, but apparently it returns a blank cell.The answer given by Hexamon to use CASE ELSE, however, works even with NULL values, so thanks for that. – Isaacson Oct 7 '15 at 9:28

I would use CASE ELSE.

Let's say you have three columns called "a", "b" and "c". You want to check column "a" if it matches the string in either "b" or "c", and on each row where it matches you want to output a 1 in another column called "bool".

Update column "bool" with this code in the field calculator:

"a" = "b" OR
"a" = "c" THEN 1 ELSE 0 END

I just tested it and it works for me.

  • I also tend to use the CASE statements quite regularly :) – Joseph Oct 5 '15 at 15:19

have you tried something like:

column1 = column3 OR column2 = column3

This is based on my reading of the Field Calculator manual as I don't currently have QGIS running.

  • 1
    This should produce the same result :) – Joseph Oct 5 '15 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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