6

I have data in a column that is causing problems. There are multiple bad characters I need to remove. I'd like to do this in the query.

On this question: MySQL string replace

I see where I can SELECT REPLACE(string_column, 'search', 'replace') as url but this only works for example replacing a / with //

I need to replace / with // and also & with && for example in a single query. What is the best way to achieve this?

Community
  • 1
  • 1
Rocco The Taco
  • 3,463
  • 9
  • 40
  • 76

1 Answers1

13

If you are replacing multiple character then you need to use multiple replace in one query something as below. But if there are many characters to be replaced then its better to use application layer to handle it. In other words for few replacement its easy to use query but for many character replacement the query really becomes messy and ends up hard to read or change.

select
replace(
  replace(string_column,'/','//'),'&','&&'
)
Abhik Chakraborty
  • 42,961
  • 5
  • 46
  • 60