1

I'm using a CASE WHEN expression in the field calculator on an existing field which already contains information. The expression is comparing two fields. If these fields match, the result should be 0. It looks something like this CASE WHEN "TD_sum" = "Qup THEN" '0' END. The field I'm editing already contains values which should stay the same if the CASE isn't true. So how do I alter the expression so it only calculates the matching fields? Is it possible to add ELSE? What do i write as the output?

I know that I can simply select the matching fields with select features and then calculate them, but maybe this calculation will be necessary for a model and I want as little individual steps as possible.

3 Answers 3

5

You can have the field you are editing in the else case:

CASE WHEN "TD_sum" = "Qup" THEN '0' ELSE "Field you are editing" END
1
  • 1
    Thanks a lot! Did not know it was that easy.
    – Alwin
    Commented Jul 9, 2020 at 9:07
2

Just add: ELSE "OutputFieldName" before the END this way it will keep the existing value in the output field if the condition is false. ( Also you need to correct your double quote, it should be before the THEN not after and if the 0 is a number you don't need quote(if you're updating a text field the '0' is right))

2

To add another possibility: in the first step, make a selection in the attribute table (select by expression) and select with an expression the fields that do NOT already contain a value. In the second step, use the field calculator to update the field and check the box "update only selected objects".

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.