OneBite.Dev - Coding blog in a bite size

Update text between two words in mysql

How to get and update text between two words in mysql. We can use the replace method and combine in with normal Where clause

How to get and update text between two words in mysql. We can use the replace method and combine in with normal Where clause.

For example, let’s say i have a simple markdown for image before but i need to prepend a new folder name since i move the resources folder. To replace a text in “columnName” that put between [‘img’] and [‘/img’] i use REPLACE(columnName, part text you want to change, new text you want to insert)

UPDATE tableName  
SET columnName = REPLACE(columnName, '\[img\]', '\[img\]newfolder/') WHERE columnName LIKE '%\[img\]%' AND columnName LIKE '%\[/img\]%'  

You can adjust the above code with your needs, like changing the two words in between or the old and new text

sql database