How to change strings in MySQL tables

How to change strings in MySQL tables? e.g. I want to change domain.com to www.domain.com.

Use the REPLACE functions of MySQL: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

One example is like this:

UPDATE table
SET field = REPLACE(field, 'domain.com', 'www.domain.com')
WHERE field LIKE '%domain.com%'

The WHERE clause is not needed but can make execution faster.

Leave a Reply

Your email address will not be published. Required fields are marked *