Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Remove substring from an entry

My 'Location' table contains 6 columns. (ID, Name, Alias, Area, X, Y)

Example of some entries from the 'Name' column:

Blackwood Highschool, Paris, France

Hilltop Market, Barcelona, Spain

Roundwell Plaza, Melbourne, Australia

Rurk Mount, Moscow, Russia(mountain)

History Museum, Prague, Czech Republic

Narrow River (river), Bombay, India

Some entries include "(mountain)", "(river)" or "(...)" within the name (8 different ones). I don't know why the table was created this way. It should have had an extra column for this data, but well.

I want to remove just the "(...)" substrings from the Location Names. I don't know how to do it, it's something like this so you get an idea:

DELETE FROM 'Location' 
WHERE 'Name'
LIKE '%(%)%';

I know this would delete the whole row, but I just want to remove the (%) term from the 'Name' string.

like image 282
Mazka Avatar asked Jun 21 '13 20:06

Mazka


Video Answer


2 Answers

If you only have 8 variations, and this is a one time thing, you could do it with a replace.

update location
  set name = replace(name , '(river)','')
  where name like '%(river)%';
like image 174
Brian Hoover Avatar answered Oct 12 '22 22:10

Brian Hoover


You can do this with brute force string operations:

select concat(left(name, instr(name, '(') - 1),
              right(name, length(name) - instr(val, ')'))
             )

Actually, you want this in an update statement:

update location
    set name = concat(left(name, instr(name, '(') - 1),
                      right(name, length(name) - instr(val, ')'))
                     )
    where name like '%(%)%';

You do not want delete, because that deletes entire rows.

like image 39
Gordon Linoff Avatar answered Oct 13 '22 00:10

Gordon Linoff