views:

83

answers:

3

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'

+4  A: 
UPDATE `table` SET `field` = REPLACE(`field`, `string`, `anothervalue`)
Tatu Ulmanen
+1  A: 

use a LIKE operator to find the rows that you care about, and update them using the REPLACE function.

Bernard Chen
FYI: code > explanation, but both is ideal.
OMG Ponies
+3  A: 

I think this should work:

update table set field = REPLACE(field, 'string', 'anothervalue') WHERE field LIKE '%string%';
Kaleb Brasee
Of course... thank you... Once upon a time, I knew about this function... stupid flu.
n00b0101