9

I am working with land use data in QGIS and have multiple values in a field (let's call the field "zoning") that I would like to categorize into a broader parent field called "land_use". So, there are multiple values in the "zoning" field that can result in one value in the new "land_use" field. For instance, values "A1", "A2" and "E" in the "zoning" field should be labelled as "Residential" in the new "land_use" field. I also have many other values in the zoning field that I need to recategorize into the land use field and would like to do this using one expression if possible (e.g., "CO1" or "CEW" as "Commercial", etc.).

Following another thread, I wrote this code:

CASE
 WHEN "stat_land_" = 'A1' OR "stat_land_" = 'A2' OR "stat_land_" = 'A9' OR "stat_land_" = 'E' OR "stat_land_" = 'E2'
 THEN 'Residential|'
END
||
CASE
 WHEN "stat_land_" = 'C' OR "stat_land_" = 'C1'
 THEN 'Vacant Land|'
END
||
CASE
 WHEN "stat_land_" = 'D1' OR "stat_land_" = 'D2' OR "stat_land_" = 'D3' OR "stat_land_" = 'E1' OR "stat_land_" = 'E3'
 THEN 'Agriculture|'
END
||
CASE
 WHEN "stat_land_" = 'C04'
 THEN 'Commercial|'
 ELSE 'Other'
END 

However, all my values end up in NULL. What am I doing wrong?

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Nov 21, 2021 at 5:25
0

1 Answer 1

17

Use like this:

CASE
 WHEN "stat_land_" IN ('A1', 'A2', 'A9', 'E', 'E2')
 THEN 'Residential' 
 
 WHEN "stat_land_" IN ('C', 'C1')
 THEN 'Vacant Land'
 
 WHEN "stat_land_" IN ('D1', 'D2', 'D3', 'E1', 'E3')
 THEN 'Agriculture'
 
 WHEN "stat_land_" = 'C04'
 THEN 'Commercial'
 
 ELSE 'Other'
 
END 

The problem is probably about operator ||.

operator ||

Joins two values together into a string.
If one of the values is NULL the result will be NULL. See the CONCAT function for a different behavior.

answered Nov 21, 2021 at 8:34
1
  • 6
    to avoid typing the field name multiple times, you can use the IN operator: ``` CASE WHEN "stat_land_" IN ('A1', 'A2', 'A9', 'E', 'E2') THEN 'Residential' WHEN "stat_land_" IN ('C', 'C1') THEN 'Vacant Land' WHEN "stat_land_" IN ('D1', 'D2', 'D3', 'E1', 'E3') THEN 'Agriculture' WHEN "stat_land_" = 'C04' THEN 'Commercial' ELSE 'Other' END ``` Commented Nov 21, 2021 at 8:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.