MySql - Way to update portion of a string?

n00b0101 picture n00b0101 · Dec 9, 2009 · Viewed 79.4k times · Source

I'm looking for a way to update just a portion of a string via MySQL query.

For example, if I have 10 records all containing 'string' as part of the field value (i.e., 'something/string', 'something/stringlookhere', 'something/string/etcetera', is there a way to change 'string' to 'anothervalue' for each row via one query, so that the result is 'something/anothervalue', 'something/anothervaluelookhere', 'something/string/etcetera', is there a way to change 'anothervalue'

Answer

Kaleb Brasee picture Kaleb Brasee · Dec 9, 2009

I think this should work:

UPDATE table
SET field = REPLACE(field, 'string', 'anothervalue')
WHERE field LIKE '%string%';