MySQL - How to find and replace text as a part of a string?

Try this query:

UPDATE
    table_name
SET
    column_name = REPLACE(column_name, 'text to find', 'text to replace with')
WHERE
    column_name LIKE '%text to find%';

Example:

You have table profile with one column profile_url containing these urls:

https://facebook.com/a
https://facebook.com/aa
https://facebook.com/aaa
https://facebook.com/aaaa
https://facebook.com/aaaaa

And you want to replace facebook.com with instagram.com:

UPDATE
    profile
SET
    profile_url = REPLACE(profile_url, 'facebook.com', 'instagram.com')
WHERE
    profile_url LIKE '%facebook.com%';

All done!


*Need a good GUI Tool for MySQL? TablePlus is a modern, native tool with an elegant UI that allows you to simultaneously manage multiple databases such as MySQL, PostgreSQL, SQLite, Microsoft SQL Server and more.
*

Download TablePlus free.

TablePlus GUI Tool MySQL

← All posts