Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove sub string from a column's text

Tags:

sql

postgresql

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
.... | ..... 
.... | ..... 
like image 895
Mithun Sreedharan Avatar asked Feb 28 '12 06:02

Mithun Sreedharan


2 Answers

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 
like image 163
lvil Avatar answered Nov 05 '22 06:11

lvil


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,'')
like image 44
Vikram Avatar answered Nov 05 '22 07:11

Vikram