Thursday, September 8, 2011

A MySQL Query to Replace a Character in a String

I wrote an import script for our old application to the new application database recently. While testing the import today, I noticed that a certain field used comma delimiters in the old database, but used ||| in the new, and my conversion script hadn't taken that into account. I needed a quick way to replace all the commas that came over in the input with pipes. Here is the query that did it for me:
UPDATE `table` SET fieldname = REPLACE(fieldname,',','|||');
Worked like a charm.