MySQL search and replace some text in a field

Viewed 233935

What MySQL query will do a text search and replace in one particular field in a table?

I.e. search for foo and replace with bar so a record with a field with the value hello foo becomes hello bar.

7 Answers

Change table_name and field to match your table name and field in question:

UPDATE table_name SET field = REPLACE(field, 'foo', 'bar') WHERE INSTR(field, 'foo') > 0;
UPDATE table_name 
SET field = replace(field, 'string-to-find', 'string-that-will-replace-it');
Related