Joining columns in a PostgreSQL SELECT statement
When concatenating character strings in a PostgreSQL SELECT statement, you may encounter errors if the columns are not explicitly converted to text.
Question:
There are two string columns a and b in table foo. Trying to join them using a || b or a || ', ' || b returns null or unexpected results.
Solution:
To properly concatenate strings in Postgres, at least one input must be converted to text. Here are two ways to do this:
SELECT a::text || b AS ab FROM foo;
SELECT a || ', ' || b AS ab FROM foo;
Note:
SELECT concat_ws(', ', a, b) AS ab FROM foo;
SELECT concat(a, b) AS ab FROM foo;
Additional notes:
The above is the detailed content of How to Properly Concatenate Columns in PostgreSQL SELECT Statements?. For more information, please follow other related articles on the PHP Chinese website!