MySQL : how to remove double or more spaces from a string?

前端 未结 11 1287
南方客
南方客 2020-12-03 08:12

I couldn\'t find this question for MySQL so here it is:

I need to trim all double or more spaces in a string to 1 single space.

For example: \"The  

11条回答
  •  一生所求
    2020-12-03 08:17

    If the string that you want to convert consists of only alphabets and multiple number of spaces [A-Za-z ]* then the following function will work. I found out a pattern when such strings are converted to hex. Based on that my solution follows. Not so elegant, but it doesn't require any procedures.

    unhex(
    replace(
    replace(
    replace(
    replace(
    replace(
    replace(
    hex(str)
    ,204,1014)
    ,205,1015)
    ,206,1016)
    ,207,1017)
    ,20,'')
    ,101,20)
    )
    

提交回复
热议问题