Replace String In Mysql With One Query

We know how to change data inside a field by using update command inside a query. Here we will try to replace part of the data without changing the entire data of the field. The SQL query we design will work like string replacement in any script we use. For this we will use replace command to replace a part of the data inside a field. Here is the format:

UPDATE `table_name` SET `field_name` = REPLACE(`field_name`, ‘Keyword to be replaced’, ‘Replacement String’);

For example, if we want to replace the string “<” with an actual less-than angle bracket in all records that have “<” in the “articleItem” column.

UPDATE articles SET articleItem = REPLACE(articleItem, ‘&lt;’, ‘<‘)

Permanent link to this article: https://blog.openshell.in/2011/01/replace-string-in-mysql-with-one-query/

Leave a Reply

Your email address will not be published.