Whatever the actual types behind the curtain are, you can "fix" your statement by casting to text
(any type can be cast to text
):
SELECT a::text || ', ' || b::text AS ab FROM foo;
If NULL
values can be involved, you may want to use the concat_ws()
function (Postgres 9.1 or later):
SELECT concat_ws(', ', a::text, b::text) AS ab FROM foo;
Or just concat()
if you don't need to add separators:
SELECT concat(a::text, b::text) AS ab FROM foo;
More details (and why COALESCE
is a poor substitute) in this related answer:
Regarding update in the comment:
+
is not a valid operator for string concatenation in Postgres (or the SQL standard). It's a a private idea of Microsoft to add this to their products.
There is hardly any good reason to use character(n)
. Use text
or varchar
. Details:
Either way, the statement would just work in modern day Postgres with any character type. Try:
SELECT 'a '::character(2) || 'b '::character(2)
You must be involving other data types.
a
a string ? – Raphaël Althaus Nov 13 '13 at 0:26text
type? – PM 77-1 Nov 13 '13 at 0:31concatenate
I doubt that he's dealing with numeric types, though PostgreSQL would take care of some of them as well. See here:postgresql.org/docs/9.1/static/functions-string.html – PM 77-1 Nov 13 '13 at 0:34+
, but the question displays the correct concatenation operator||
. The OP must post the exact data types of the involved columns, the exact statement and the exact error message. And possibly if NULL values are involved. The problem itself is trivial. – Erwin Brandstetter Nov 13 '13 at 0:53+
was suggested by another poster in now deleted comment. Otherwise I fully agree. – PM 77-1 Nov 13 '13 at 1:00