Search code examples
sqlpostgresqltypesconcatenationcoalesce

How to concatenate columns in a Postgres SELECT?


I have two string columns a and b in a table foo.

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

Update from comments: both columns are type character(2).


Solution

  • 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;