In order to concatenate two columns in postgresql we use CONCAT() function, we use CONCAT function in postgresql to concatenate two columns to one column. Let’s see how to
- Concatenate two character columns in postgresql.
- Concatenate character and numeric / integer column in postgresql.
We will be using Zipcodes table.
Concatenate two columns in postgresql (two character column) :
Lets concatenate city and state column as shown below
select *,concat(city,state) as city_state from zipcodes
So the resultant table will be
Concatenate two columns in postgresql (two character column with hyphen):
Let’s concatenate city and state column as shown below
select *,concat(city,' - ',state) as city_state from zipcodes
So the resultant table will be
Concatenate two columns in postgresql (character and integer column with hyphen):
Let’s concatenate city and zip column as shown below. Zip column is integer column so we will be typecasting them to character column before concatenating.
SELECT *,city || zip::text AS city_zip FROM zipcodes;
So the resultant table will be