Concatenate two columns in Postgresql

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 1

 

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 2

 

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 3

 

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

Concatenate two columns in Postgresql 4

 

                                                                                         

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.