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?

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.

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 ```
    – pigreco
    Nov 21, 2021 at 8:56

Your Answer

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

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