joshweir joshweir - 3 months ago 11x
MySQL Question

Replace duplicate adjacent characters with single character using mysql

Using mysql, this query:

select "-foo---baz-beer--" as result

I want to transform to replace multiple adjacent occurrences of
with its single counterpart. Psuedocode:

select custom_replace("-foo---baz-beer--") as result

Would return this:


How do do this with mysql?


Here is a trick you can use for this purpose:

select replace(replace(replace(col, '-', '><'), '<>', ''), '><', '-')