5

I need to label some polygons with 2 fields using only one label:

The first being a field in the attribute table named "Bio-Code" the second one being optional if a cell in the field "BBT-Code" is filled with numbers.

The first field and second field should be separated by a slash "/" (not the slash used as operator)

example

I have yet not been successful at creating an appropriate expression. Could you point out where I have made mistakes?

Bio_Code
/
CASE
WHEN "BBT_CODE" IS '12500' THEN '12500'
WHEN "BBT_CODE" IS '12714' THEN '12714'
WHEN "BBT_CODE" IS '715212' THEN '715212'
WHEN "BBT_CODE" IS '715222' THEN '715222'
WHEN "BBT_CODE" IS NULL THEN 'None'
END
  • 4
    If you use Single labels instead of rule-based, you could try the following expression: if("BBT_CODE" is not NULL, "Bio_Code" || ' / ' || "BBT_CODE", "Bio_Code") – Joseph Nov 4 at 10:54
  • 2
    @Joseph has it correct, this is way easier. – Erik Nov 4 at 10:58
  • hmmm, thanks :) This leads to another question which I am struggling with: How do I make these 2 labels placed next to each other? The "offset" function seems to be working with the background only... Also QGIS shows only 1 label even if I use the "placement" function and chosing "around centroid" for one label and "offset from centroid" for the other... – Antje Holderbach Nov 4 at 11:20
  • 2
    @AntjeHolderbach Joseph's solution does create a single label, not two, therefore I don't understand your question. If it is important, maybe ask a separate, new question? – Erik Nov 4 at 12:22
  • 1
    I misunderstood @Joseph and after some more thinking the penny finally dropped and I tried what he was suggesting and it worked like a charm :) – Antje Holderbach Nov 5 at 8:42
7

If you use Single labels instead of rule-based, you could concatenate more than one field using ||. Couple this with an if statement, you could try the following expression:

if("BBT_CODE" is not NULL, "Bio_Code" || ' / ' || "BBT_CODE", "Bio_Code")
  • I need to use rule based labeling because I need to have a different couloured background according to a value. See my other question here: gis.stackexchange.com/questions/332720/… – Antje Holderbach Nov 4 at 11:32
  • 1
    @AntjeHolderbach as the second answer to your linked question states, you may base your label background on values of the attribute table. – Erik Nov 4 at 12:23
  • 1
    this method will work with rule-based labeling just as well as it does with single labels. – csk Nov 4 at 17:15

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.