I have a vector with different land use classes for which I want to add labels that correspond to their number. I am trying to use to QGIS field calculator, I have the code to replace one value into a new column, but I want to do it for all the independent variables. My code however seems incorrect and the label name doesn't change to what I want it to be. I have a total of six values that need a new label. I am using the code

replace("land_classes", '6', 'type 1') + replace("land_classes", '7', 'type 2')

enter image description here

1 Answer 1


It would be better to use a CASE statement Like so :

  WHEN "land_classes"='6' THEN 'type 1'
  WHEN "land_classes"='7' THEN 'type 2'
  ELSE 'Not a valid land use class'

Note that I assume that the "land_classes" is a text field so the value are between quote (as you did in your expression), if the "land_classes" field is rather a numeric field (by your picture that seem likely) you should not use quote around the value (so change ='6' to =6 ).

Also made sure to set the output field to string type.

  • Thanks J.R. this did exactly the trick
    – Hartje
    Commented Sep 4, 2023 at 14:01

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.