SQL: Search and Replace Text

The way to do a "search and replace" function in SQL is to use the UPDATE command along with the REPLACE command.

The syntax is:
UPDATE table_name SET column_name = REPLACE ('text sought','new text');

For example, I have some old CMS posts that have missing triangle gifs, and I want to replace them with star gifs in WordPress:

UPDATE wp_posts SET post_content = REPLACE (post_content, '/feature/film_reviews/triangle.gif'', '/wp-content/uploads/2015/01/star.gif'');

You should probably do a backup of your database first, and will probably want to test out your code on a single record first, by adding a WHERE clause like: WHERE ID= 99999, or WHERE ID BETWEEN 1000 AND 1005.

No comments :

Post a Comment