57

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?

2
  • 3
    The reason for this is because I knew I would want to symbolise the data using letters, as I will be performing some kind of data mining on the table and the data mining code I have takes strings. Commented Mar 13, 2013 at 14:53
  • Sorry to use an answer to ask a question but is there a way to reverse the process in answer 1? (can't comment). Because I performed the update but in the where clause I mispelled the similar to operator value and it added the text to ALL the entries. Thanks! Commented Apr 17, 2020 at 21:11

3 Answers 3

98

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, this seems to be what I'm looking for!
it's not working if the columns initial value is null :(
What do you mean by "not working"? Any error messages? If you are looking for a different behaviour you can try the concat function instead: postgresql.org/docs/9.1/static/functions-string.html
@subbu Not sure what kind of suggestion you are looking for. String operations with null don't make sense. You can skip the null values (where column is not null), transform null into an empty string before concating, or whatever is best in your special case. There is no general solution.
@SebastianvomMeer Thanks. Yes tried with concat_ws(...) and COALESCE(...). These solved my problem.
|
1

The accepted answer is not handle null value and blank space. So I gave this answer. If it help someone, it will be my pleasure.

update "public"."mytable" set 
"name"= case when "name" is null or trim("name")='' then null else 'a' || "name" end,
"age"= case when "age" is null or trim("age")='' then null else 'b' || "age" end,
"location"= case when "location" is null or trim("location")='' then null else 'c' || "location" end;

Comments

-1

As of version 9.1, PostgreSQL provides a built-in concatenation function called CONCAT(). The Concat function in Postgres appends text to the end of the value and can handle blank values as well.

UPDATE mytable
SET name=Concat(name, 'a');

Hope this helps ;)

1 Comment

why was this down voted?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.