I'm trying to create labels for features, based on the value (or missing value) of three different columns and combinations can be possible.
I have three columns: Veg_dom, Veg_codom and Veg_onderg.
When all three columns have a value for a feature, I want the label to be as follows:
Veg_dom$value + Veg_codom$value + (Veg_onderg$value)
.
Other possibilities should be
Veg_dom$value + Veg_codom$value & Veg_dom$value + (Veg_onderg$value)
.
I use the following expression in the label expression window:
The labels however show as follows: enter image description here
The only one correct is the first statement if all three columns have values.
Am I doing something wrong? Are nested IF
statements allowed?
Expression:
if( Veg_codom <> ' ' AND Veg_onderg <> ' ',
Veg_dom + '+' + Veg_codom + '+' + concat('(', Veg_onderg, ')'),
if( Veg_codom = ' ' AND Veg_onderg <> ' ',
Veg_dom + '+' +concat('(', Veg_onderg, ')'),
if( Veg_codom <> ' ' AND Veg_onderg = ' ',
Veg_dom + '+' + Veg_codom,
Veg_dom)))
Cels hold character-data:
-
It'd be helpful to know, what values your columns hold - especially how fields without values look like.Erik– Erik2020年08月13日 15:22:42 +00:00Commented Aug 13, 2020 at 15:22
-
Thanks both. I added the expression as text and added a picture and description of the data in the cells.Stevestingray– Stevestingray2020年08月13日 16:43:45 +00:00Commented Aug 13, 2020 at 16:43
-
2Please don't post an answer in the top of your question.Vince– Vince2020年08月13日 22:33:17 +00:00Commented Aug 13, 2020 at 22:33
2 Answers 2
I think you need a CASE
statement, which will allow you to have different conditions.
Something like:
CASE
WHEN Veg_codon <> '' AND Veg_ondeg <> '' THEN Veg_dom + Veg_codom + '('+Veg_onderg+')'
WHEN Veg_codon <> '' AND Veg_ondeg = '' THEN Veg_dom + Veg_codom + '(some default value)'
WHEN Veg_codon = '' AND Veg_ondeg <> '' THEN Veg_dom + 'default value' + '('+Veg_onderg+')'
...
END
Solution: Both the IF statements and the CASE statement work. I misused the = '' sign where I should have used IS NULL, to specify a cell is empty. So:
if( Veg_codom <> ' ' AND Veg_onderg <> ' ',
Veg_dom + '+' + Veg_codom + '+' + concat('(', Veg_onderg, ')'),
if( Veg_codom IS NULL AND Veg_onderg <> ' ',
Veg_dom + '+' +concat('(', Veg_onderg, ')'),
if( Veg_codom <> ' ' AND Veg_onderg IS NULL,
Veg_dom + '+' + Veg_codom,
Veg_dom)))