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.
2 Answers
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
-
2There is also the
concat
function in the dev versions. Using || doesn't handle NULL, iflink
is NULL the whole label is nullconcat
doesn't have this issue. || is taken from Postgres postgresql.org/docs/9.1/static/functions-string.html– Nathan WCommented Jul 12, 2012 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? Commented Jul 12, 2012 at 12:22
-
1
-
3You are missing END. The correct string is
CASE WHEN ("link" IS NULL) THEN '(nodata)' ELSE "link" END
– Nathan WCommented Jul 12, 2012 at 12:48 -
1
On QGIS veriosn 2.0.1 the simple expression worked like this:
"field1" || ' (' || "field2" || ')'
desired output: field1 (field2)