MySql String replace with REPLACE()

Often i have to replace clearly identifiable strings (strings, text) with a new string in a MySQL column. Either its possible to use php, read the DB content and use str_replace() for string replacement, there is a much better way:

Thank God, MySQL offers a native REPLACE() function therefore:


Returns the string str with all occurrences of the string from_str replaced by the string to_strREPLACE()performs a case-sensitive match when searching for from_str.

In this example i replace all appearance of the string [youtube] with an empty string in the column xf_post of my XenForo database:

UPDATE xf_post SET message = REPLACE(message, '[/youtube]', '') WHERE message LIKE '%[/youtube]%';



Written by

"Enthusiastic Sim-Racer from the north of germany. I write here about web development, hardware, software and latest motion simulator technologies."