MySql - Way to update portion of a string?

MysqlSqlStringSql Update

Mysql Problem Overview


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'

Mysql Solutions


Solution 1 - Mysql

I think this should work:

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

Solution 2 - Mysql

UPDATE `table` SET `field` = REPLACE(`field`, 'string', 'anothervalue')

Solution 3 - Mysql

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%'

Solution 4 - Mysql

Does something like this work in any way?

update table_name
set column_name = replace(column_name, 'string%', 'string') 
where column_name like '%string%'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionn00b0101View Question on Stackoverflow
Solution 1 - MysqlKaleb BraseeView Answer on Stackoverflow
Solution 2 - MysqlTatu UlmanenView Answer on Stackoverflow
Solution 3 - MysqlBernard ChenView Answer on Stackoverflow
Solution 4 - MysqlLukasView Answer on Stackoverflow