Find and Replace with MySql

As exemplified below, MySQL has a very useful function for finding and replacing data.

update [table_name] set [field_name] = replace([field_name], '[search_data]', '[replace_data]');

A real-world example would look something like this:

update page set content = replace(content, 'http://preview.sitehatchery.com', 'http://www.sitehatchery.com');

The nice thing is that the replace function replaces only the part of the string that matches. For instance, if there were multiple instances of "http://preview.sitehatchery.com" in the "content" field of a given record, it would only replace the matching string segments. The surrounding data in the same field would remain unchanged.

Sitehatchery.com is a small website design company located in Chico, California, offering high-quality affordable websites. As the web site designer, website developer or programmer, and Web Master, Jason Cox builds custom websites and dynamic programming solutions using PHP, JavaScript, HTML, CSS, AJAX and Flash, in Northern California and abroad. In addition to web development and website design, services include domain name management, hosting, SEO reports, as well as free PHP articles, HTML articles, CSS articles, and more.