2

This question already has an answer here:

I am new to QGIS (ESRI shop at work). I am attemping to concatenate fields in the label expression

field1 = number
field2 = number

label expression I tired but fails

str(field1) + " - " + str(field2)

it says its not valid. This would work in ArcMap... how is it different?

marked as duplicate by underdark qgis Apr 12 '18 at 17:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

You need to change three things about your query:

  • The conversion to String is done by to_string rather than str
  • Strings are surrounded with ' rather than "
  • Field names are surrounded with "

So your query should look something like this:

to_string("field1") + ' - ' + to_string("field2")

  • 2
    Shouldn't it be: to_string("field1") || ' - ' || to_string("field2") – HeikkiVesanto Apr 11 '18 at 15:47
  • 1
    Yes, maybe that would be nicer, since it's proper SQL. It works both in QGIS, though. – karpfen Apr 11 '18 at 15:58
  • 1
    Cool. Must be a new feature. Back in my day (1.8) we had to use the double bar. – HeikkiVesanto Apr 11 '18 at 16:15
  • 1
    Yep, seems like it. qgis.org/en/site/forusers/visualchangelog260/… – karpfen Apr 12 '18 at 8:58
4

You can use the concatenate operator || or the concat function (under strings).

  • still says invalid... field1 + concat(" -" + field2) – NULL.Dude Apr 11 '18 at 15:37
  • concat('-',field2) – Ian Turton Apr 11 '18 at 15:38

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