6

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:

enter image description here

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:

enter image description here

3
  • It'd be helpful to know, what values your columns hold - especially how fields without values look like.
    – Erik
    Commented 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. Commented Aug 13, 2020 at 16:43
  • 2
    Please don't post an answer in the top of your question.
    – Vince
    Commented Aug 13, 2020 at 22:33

2 Answers 2

9

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
4

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)))

Your Answer

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

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