Tag Archives: SQL

MySql String replace with REPLACE()

[gard]
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:

REPLACE(str,from_str,to_str)

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]%';

[gard]