4

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

  • It'd be helpful to know, what values your columns hold - especially how fields without values look like. – Erik Aug 13 at 15:22
  • Thanks both. I added the expression as text and added a picture and description of the data in the cells. – Stevestingray Aug 13 at 16:43
  • 2
    Please don't post an answer in the top of your question. – Vince Aug 13 at 22:33
7

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
| improve this answer | |
2

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)))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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