3

I'm struggling to include the field data from a double type attribute in a label.

I've managed fine to pull in the data from the "Code" and "Features & Remarks" columns... I also wanted to encase the "Code" field in parentheses followed by a space, so used the following;

'('+"Code"+') '+"Features & Remarks"

Which also works fine when the latter column is empty. Both columns are QStrings.

Now I'm also trying to pull data in from another column "Depth of Burial" which is a double type. As a note, either "Depth of Burial" or "Features & Remarks" is always NULL - they never both have data in a given row, but often both are NULL.

sample of attribute table

'('+"Code"+') '+"Depth of burial [m]"+"Features & Remarks" gives me no labels anywhere.

'('+"Code"+') '+(coalesce("Depth of burial [m]","Features & Remarks")) only gives me labels where there is data in "Features & Remarks".

'('+"Code"+') '+(coalesce("Depth of burial [m]","Features & Remarks",' ')) displays all labels correctly EXCEPT rows with "Depth of Burial" data - they have no label.

I have also tried concatenate and if statements. The only way I could display the "Depth of Burial" column data was to either call it on its own or in the following;

if("Depth of burial [m]"=NULL,'('+"Code"+') '+"Features & Remarks","Depth of burial [m]")

However this one did not show any labels other than the "Depth of Burial" values.

My ideal aim to generate labels that are all prefixed with the "Code" column in parentheses then contain either the "Depth of Burial" value or the "Features and Remarks" column depending on which has data for a given row.

3

1+1 is a number, 2.

A+B is a concatenated text, AB

But what should happen when A+1 is computed? Should it be a concatenated text, or an invalid operation? QGIS will first try to convert the string to a number, then to do the addition, which fails as A is not a number. It would however succeed with '1'+2 = 3.

To overcome this behavior, you can either use the concatenation operator || instead of +, or you can convert the number to text.

'('+"Code"+') ' || (coalesce("Depth of burial [m]","Features & Remarks"))
'('+"Code"+') ' + to_string(coalesce("Depth of burial [m]","Features & Remarks"))
4
  • Thank you very much! That worked to bring in the "Depth of burial [m]" column along with the DOB code value, however I then found that rows which had NULL in both "Depth of burial [m]" and "Features & Remarks" returned no label (rows 18-20). So I added in ' ' to the coalesce expression (adding in a space at the end of the label I think?) like what sort of worked on my third example above, and that did the trick. – petehe666 Oct 19 '20 at 22:03
  • '('+"Code"+') ' || (coalesce("Depth of burial [m]","Features & Remarks",' ')) – petehe666 Oct 19 '20 at 22:04
  • '('+"Code"+') ' + to_string(coalesce("Depth of burial [m]","Features & Remarks",' ')) – petehe666 Oct 19 '20 at 22:05
  • I'm sure my last little bit isn't the most elegant solution, but it did the job for what I need. Thanks again! – petehe666 Oct 19 '20 at 22:06

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.