TransWikia.com

Unable to join double type data into a label expression

Geographic Information Systems Asked by petehe666 on January 10, 2021

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.

One Answer

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"))

Correct answer by JGH on January 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP