Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append text to column data based on the column in PostgreSQL

Tags:

postgresql

I would like to append some text to every cell in each column of my table to act as a symbol for that particular column. For example say my table is as follows (all fields are type character varying):

 name    age    location james   45     france simon   33     usa ben     76     china 

I would like to modify it to be:

 name    age    location ajames   b45     cfrance asimon   b33     cusa aben     b76     cchina 

Does anyone have any suggestions as to how I can do this?

like image 517
James Elder Avatar asked Mar 08 '13 12:03

James Elder


1 Answers

First you have to transform your age to be some kind of string. After that you can transform the values like this (of course you have to do this for each field):

update mytable set name = 'a' || name, age = 'b' || age; 

This updates the data inside your table. If you only want the output to be prefixed you can use the following approach:

select 'a' || name as name, 'b' || age as age from mytable; 

In this case there is no need to convert your age data type.

like image 200
Sebastian vom Meer Avatar answered Sep 21 '22 21:09

Sebastian vom Meer