MySql - Way to update portion of a string?
Asked Answered
W

3

114

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, is there a way to change string to anothervalue for each row via one query?

I.e. for the following:

Change something/string, something/stringlookhere, something/string/etcetera to something/anothervalue, something/anothervaluelookhere, something/string/etcetera

Waiwaif answered 9/12, 2009 at 20:44 Comment(0)
R
261

I think this should work:

UPDATE table
SET field = REPLACE(field, 'string', 'anothervalue')
WHERE field LIKE '%string%';
Roughandready answered 9/12, 2009 at 20:47 Comment(0)
P
27
UPDATE `table` SET `field` = REPLACE(`field`, 'string', 'anothervalue')
Proficiency answered 9/12, 2009 at 20:47 Comment(0)
C
14

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

For example:

UPDATE table_name SET field_name = REPLACE(field_name,'search','replace') WHERE field_name LIKE '%some_value%'
Carbonyl answered 9/12, 2009 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.