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

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Aug 13, 2020 at 15:18
3
  • It'd be helpful to know, what values your columns hold - especially how fields without values look like. 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. 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
answered Aug 13, 2020 at 15:28
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)))
answered Aug 15, 2020 at 10:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.