17

I've the following two columns in Postgres table

name | last_name
----------------
AA | AA aa
BBB | BBB bbbb
.... | ..... 
.... | ..... 

How can I update the last_name by removing name text from it?

final out put should be like

name | last_name
----------------
AA | aa
BBB | bbbb
.... | ..... 
.... | ..... 
Jonathan Hall
80.5k19 gold badges163 silver badges207 bronze badges
asked Feb 28, 2012 at 6:53

3 Answers 3

21
UPDATE table SET last_name = regexp_replace(last_name, '^' || name || ' ', '');

This only removes one copy from the beginning of the column and correctly removes the trailing space.

Edit

I'm using a regular expression here. '^' || name || ' ' builds the regular expression, so with the 'Davis McDavis' example, it builds the regular expression '^Davis '. The ^ causes the regular expression to be anchored to the beginning of the string, so it's going to match the word 'Davis' followed by a space only at the beginning of the string it is replacing in, which is the last_name column.

You could achieve the same effect without regular expressions like this:

UPDATE table SET last_name = substr(last_name, length(name) + 2);

You need to add two to the length to create the offset because substr is one-based (+1) and you want to include the space (+1). However, I prefer the regular expression solution even though it probably performs worse because I find it somewhat more self-documenting. It has the additional advantage that it is idempotent: if you run it again on the database it won't have any effect. The substr/offset method is not idempotent; if you run it again, it will eat more characters off your last name.

answered Feb 28, 2012 at 6:58
Sign up to request clarification or add additional context in comments.

4 Comments

What about the spaces between the words?
Yeah, that's the trailing space I handle by adding || ' ' to the end. Do you have other whitespace issues than what one would infer from your question?
@Daniel Lyons: Could you please explain how your regexp works? thanks
@Daniel Lyons A little explanation would be appreciated, like how it handles Davis McDavis
13

Not sure about syntax, but try this:

UPDATE table 
SET last_name = TRIM(REPLACE(last_name,name,'')) 

I suggest first to check it by selecting :

 SELECT REPLACE(last_name,name,'') FROM table 
answered Feb 28, 2012 at 6:58

3 Comments

Close, but two problems: not handling spaces or if the firstname occurs in the lastname. E.g., if my name were Davis McDavis, his original text would be 'Davis McDavis' and you'd change it to ' Mc'.
What about the spaces between the words?
@Mithun: Added TRIM to handle spaces. Daniel's problem remain
6

you need the replace function see http://www.postgresql.org/docs/8.1/static/functions-string.html

UPDATE table SET last_name = REPLACE(last_name,name,'')
answered Feb 28, 2012 at 7:00

2 Comments

What about the spaces between the words?
you can use: trim(both ' ' from REPLACE(last_name,name,''))

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.