Search and replace in a MySQL table

Here is a little “trick” I learned last week. If you have a MySQL table in which you need to change strings into other strings upi can use the REPLACE function.

You might need this when you have a BLOG or CMS where you have hardcoded url’s in your posts that all need to change. In my case, I had to replace some weird characters that got changed after a migration.

The replace syntax is as follows:

update table_name set table_field = replace(table_field,'replace_that','with_this');

So an example query could be:

update blogposts set postcontent = replace(`postcontent`,'my-old-url.com','my-new-url.com');

Related articles you might like:

Leave a Reply »»