Using REPLACE in an UPDATE statement

REPLACE takes in three strings as arguments and returns a string as a result.

REPLACE(item1, item2, item3)

item1 is our original string. item2 is the subtring we want replaced. item2 is the string that will replace item2 is the resultant string. This resultant string is represented by the REPLACE function itself. The following example shows how the REPLACE function works.

Let’s say you have thousands of posts that contain your site’s link, ex: and you want to change it to a sub-domain link, ex:, the SQL command bellow will do exactly what we want to achieve:

UPDATE posts_table SET content = replace(content, '', '') WHERE content LIKE '%';

posts_table – Name of the table that contains all your posts
content – Column name that contains the contents of your posts

Do you need help with a project? or have a new project in mind that you need help with?

Contact Me