Skip to content
Advertisement

Can MySQL replace multiple characters?

I’m trying to replace a bunch of characters in a MySQL field. I know the REPLACE function but that only replaces one string at a time. I can’t see any appropriate functions in the manual.

Can I replace or delete multiple strings at once? For example I need to replace spaces with dashes and remove other punctuation.

Advertisement

Answer

You can chain REPLACE functions:

This will print hi earth.

You can even use subqueries to replace multiple strings!

Or use a JOIN to replace them:

I’ll leave translation using common table expressions as an exercise for the reader 😉

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement