16

I'm using QGIS 1.8 on windows XP machine and I try to do a concatenation of several fields to label the names of my local roads in the attribute table, but I can not find the correct syntax. The first field consists of om the street, the second type of link, eg: Avenue and the third orientation, for example, East or West. Could you tell me how to write the correct syntax using the dialog box of labels based on a formula. Attached a screenshot that shows the attribute table.

enter image description here

20

The concatenation operator is || (odd huh?), so you would use something like:

street || ', ' || link || ', ' || orientation

Intersperse them with any separators and static strings you like.

As @NathanW noted, this will fail if any of the fields is NULL, but you can use a fallback value in that case:

street || ', ' || CASE WHEN ("link" IS NULL) THEN '(nodata)' ELSE "link" END || ', ' || orientation

QGIS supports a helper function coalesce since version 2.0. It is there to avoid the problems of NULLs and the example would look like:

 street || ', ' || coalesce(link, '(nodata)') || ', ' || orientation
| improve this answer | |
  • 2
    There is also the concat function in the dev versions. Using || doesn't handle NULL, if link is NULL the whole label is null concat doesn't have this issue. || is taken from Postgres postgresql.org/docs/9.1/static/functions-string.html – Nathan W Jul 12 '12 at 12:07
  • Looks like it doesn't support case statements directly. Is the only option without using SQL directly to create a non-NULL copy of the link column? – lynxlynxlynx Jul 12 '12 at 12:22
  • What do you mean doesn't support case statements? – Nathan W Jul 12 '12 at 12:23
  • 3
    You are missing END. The correct string is CASE WHEN ("link" IS NULL) THEN '(nodata)' ELSE "link" END – Nathan W Jul 12 '12 at 12:48
  • 1
    ahh coalesce is the the one I meant. – Nathan W Jul 13 '12 at 12:37
3

On QGIS veriosn 2.0.1 the simple expression worked like this:

"field1" || ' (' || "field2" || ')'

desired output: field1 (field2)

| improve this answer | |

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.