Can MySQL replace multiple characters? Can MySQL replace multiple characters? sql sql

Can MySQL replace multiple characters?


You can chain REPLACE functions:

select replace(replace('hello world','world','earth'),'hello','hi')

This will print hi earth.

You can even use subqueries to replace multiple strings!

select replace(london_english,'hello','hi') as warwickshire_englishfrom (    select replace('hello world','world','earth') as london_english) sub

Or use a JOIN to replace them:

select group_concat(newword separator ' ')from (    select 'hello' as oldword    union all    select 'world') originner join (    select 'hello' as oldword, 'hi' as newword    union all    select 'world', 'earth') trans on orig.oldword = trans.oldword

I'll leave translation using common table expressions as an exercise for the reader ;)


Cascading is the only simple and straight-forward solution to mysql for multiple character replacement.

UPDATE table1 SET column1 = replace(replace(REPLACE(column1, '\r\n', ''), '<br />',''), '<\r>','')


I've been using lib_mysqludf_preg for this which allows you to:

Use PCRE regular expressions directly in MySQL

With this library installed you could do something like this:

SELECT preg_replace('/(\\.|com|www)/','','www.example.com');

Which would give you:

example