I am trying to fill out a field based on information in a different field in the same layer using a CASE Statement in the Field Calculator. My table structure looks like this:
SPECIES | SHORT |
---|---|
Toad | NULL |
NULL | B |
Fish | NULL |
Plant | NULL |
My goal looks like this:
SPECIES | SHORT |
---|---|
Toad | T |
NULL | B |
Fish | F |
Plant | P |
The Expression I am using and does not work looks like this:
CASE
WHEN "Species" LIKE 'Toad' THEN 'T'
WHEN "Species" LIKE 'Fish' THEN 'F'
WHEN "Species" LIKE 'Plant' THEN 'P'
END
The result is that everything in "SHORT"
ends up being NULL
.
The Field "SPECIES"
is a VALUE MAP widget type. "SHORT"
is a TEXT EDIT.
I am using QGIS 3.1
WHEN "Species" LIKE '%Toad%' THEN 'T'
. Otherwise, if you are looking for an exact match, dont useLIKE
, but=
instead, e.g.WHEN "Species" = 'Toad' THEN 'T'
represent_value("Species") = 'Toad'
in the expression for it to work?LIKE ...
andregexp_match
. If anything, the latter is slightly faster sinceLIKE
is translated to a regex before evaluation. Source: reading the QGIS source code. (correct me if I'm misremembering this).