2

In QGIS, Field Calculator, I am trying to fill out an existing field (target) based on values on another existing field (source), both in the same shapefile. For each value x in the source, field set the corresponding value in the target field to y.

I am trying to achieve this with a CASE statement like that: First I tick the updated existing field and set it to target. Then I use this expression

case
    when "source" is 'X' then 'Y'
end

the target filed successfully updates to Y for each X occurrence in the source field, but all the other values in the target field are set to null.

How can I prevent that from happening?

2
  • 1
    The software is functioning correctly (as you have instructed it to do). If you only want certain features updated, you should apply a WHERE constraint.
    – Vince
    Dec 13, 2021 at 12:03
  • 1
    If you want all features to get an update (or all others left untouched!), specify what ELSE to put into the field (i.e. for when "source" is not 'X'). This may simply be what's in "target" in the first place!
    – geozelot
    Dec 13, 2021 at 12:11

1 Answer 1

5

Use this expression:

case
   when "source" = 'X' then 'Y'
   else "target"
end

But why using a case statement? It's easier with an if-condition:

if ("source" = 'X' , 'Y', "target")

Your Answer

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

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