1

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.

3
  • 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, 2015 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. Oct 6, 2015 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.
    – user39790
    Oct 7, 2015 at 9:28

2 Answers 2

2

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:

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

I just tested it and it works for me.

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

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
  • 1
    This should produce the same result :)
    – Joseph
    Oct 5, 2015 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.