MySQL search and replace some text in a field

julz picture julz · Sep 24, 2008 · Viewed 207.3k times · Source

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.

Answer

Joe Skora picture Joe Skora · Sep 24, 2008

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;