QGIS – Getting Integer Based on String Value in Another Field

expressionfield-calculatorfields-attributesintegerqgis

I have a big data set with a column name "CORINE" with strings in it. I want to change these individual strings into integers and put them into a new column so that I can create a raster layer out of the created values.
I tried this code in the field calculator.

CASE 
    WHEN "CORINE" = "AG" THEN 1
    WHEN "CORINE" = "BG" THEN 2
    WHEN "CORINE" = "LW" THEN 3
    WHEN "CORINE" = "NW" THEN 4
    WHEN "CORINE" = "MW" THEN 5
    WHEN "CORINE" = "WW" THEN 6
    WHEN "CORINE" = "WA" THEN 7
    WHEN "CORINE" = "DK" THEN 8
    WHEN "CORINE" = "LN" THEN 9
    WHEN "CORINE" = "NV" THEN 11
    WHEN "CORINE" = "SU" THEN 12
    WHEN "CORINE" = "SV" THEN 13
    WHEN "CORINE" = "TM" THEN 14
    ELSE 0
END

But after the calculations, all the values in the new column are 0.
What is my mistake?

enter image description here

Best Answer

Use single quotes ' as string indicator, not ". Double quotes are used as Field indicator in QGIS field calculator. So your expression compares two fields, where the second one does not exist or is not equal to your first field, thats why it always jumps to the else case. Try this:

CASE 
  WHEN "CORINE" = 'AG' 
  THEN 1
  WHEN "CORINE" = 'BG' 
  THEN 2
  WHEN "CORINE" = 'LW' 
  THEN 3
  WHEN "CORINE" = 'NW' 
  THEN 4
  WHEN "CORINE" = 'MW' 
  THEN 5
  WHEN "CORINE" = 'WW' 
  THEN 6
  WHEN "CORINE" = 'WA' 
  THEN 7
  WHEN "CORINE" = 'DK' 
  THEN 8
  WHEN "CORINE" = 'LN' 
  THEN 9
  WHEN "CORINE" = 'NV' 
  THEN 11
  WHEN "CORINE" = 'SU' 
  THEN 12
  WHEN "CORINE" = 'SV' 
  THEN 13
  WHEN "CORINE" = 'TM'
  THEN 14  
  ELSE 0
END