Alex Narinsky Alex Narinsky - 3 months ago 13
SQL Question

How to concatenate columns in a Postgres SELECT?

I have two string columns

a
and
b
.

So
select a,b from foo
returns values
a
and
b
. However, concatenation of
a
and
b
does not work. I tried :

select a || b from foo


and

select a||', '||b from foo


OP's error message (from comments):


No operator matches the given name and argument type(s). You might
need to add explicit type casts.


He also stated that both fields are
character(2)
.

Answer

The problem was in nulls in the values; then the concatenation does not work with nulls. The solution is as follows:

SELECT coalesce(a, '') || coalesce(b, '') FROM foo;