3

Can you use "or" in "when" statements?

I have a dataset were I have to insert strings in case of numbers. example

field1: 1,2,5,6,10: 
field2:'num','left','pro','con'

I want to insert the values of field 2 in the plase of field1 as 1=num, 2=left, 5 and 6=pro, 10=pro

I have tried with: CASE When "field1"='5' or "field1"='6' then 'pro' else "field1" END

I have not found a way to insert more then one variable at the time. Can this be solved a easy way, for example by using or?

5
  • Maybe parentheses are needed? CASE When ("field1"='5' or "field1"='6') then 'pro' else "field1" END Jan 12 at 18:57
  • What GIS software are you using?
    – BERA
    Jan 12 at 19:06
  • im using QGIS 3.22
    – Endre Aaen
    Jan 12 at 19:17
  • 1
    Why not all at once? CASE when "field1"= 1 then 'num' when "field1"= 2 then 'left' else 'pro' END . However: if field1 is of fieldtype integer, you can't insert text.
    – Babel
    Jan 12 at 19:17
  • 1
    that also work. thx. Reposts this answar further down to make it easyer to find.
    – Endre Aaen
    Jan 12 at 19:48
5

The IN operator allows you to specify multiple values in a WHERE clause.

The IN operator is a shorthand for multiple OR conditions.

case
    when "field1" IN('5','6') then 'pro' --IN(5, 6) if field1 is numeric
   --more when condition goes here
    else "field1"
end
2

Why not all at once?

CASE
   when "field1"= 1 then 'num'
   when "field1"= 2 then 'left'
   else 'pro' 
END

However: if field1 is of fieldtype integer, you can't insert text.

Copy of a comment by @Babel

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.