16

I am having some issues trying to show multiple fields for a single label in QGIS. In QGIS2.6, attempting to display labels for a feature consisting of multiple fields results in an empty label if one of the fields is null. So for example, in the label expression dialog:

concat("Temp Site",'\n',"Notes",'\n',"Function")

will function normally unless one of the fields is null, then it results in no label being shown. Using the '||' operator instead in the expression dialog also results in the same behaviour:

"Temp Site" || '\n'  ||  "Notes" || '\n'  ||  "Function"

Is there a way to display the labels even if one of the fields is null?

2 Answers 2

25

Use the "Coalesce" function. Coalesce takes the first non-null value from its arguments. So this expression should work:

coalesce("Temp Site",'') || '\n'  ||  coalesce("Notes",'') || '\n'  ||  coalesce("Function",'')
1
  • 2
    You can also wrap the new line within the coalesce function, if it makes sense to avoid the blank lines in the middle of the label I used an expression like coalesce("Temp Site", ' ') || coalesce(('\n' || "Notes"), ' ') || coalesce(('\n' || "Function"), ' ' ) Jan 22, 2018 at 10:39
3

Update: This is no longer a problem. In QGIS 2.10 and greater Concat ignores null values and coalesce is no longer needed. See this answer instead Handling NULL values in labeling?

Your Answer

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

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