Remove/replace special characters in column values?

前端 未结 2 1708
野趣味
野趣味 2021-01-13 17:00

I have a table column containing values which I would like to remove all the hyphens from. The values may contain more than one hyphen and vary in length.

Example: f

相关标签:
2条回答
  • 2021-01-13 17:36

    You can use this.

    update table set column = format('%s%s', left(column, 3), right(column, -6));

    Before:

    After:

    0 讨论(0)
  • 2021-01-13 17:45

    You can use the regexp_replace function to left only the digits and letters, like this:

    update mytable
       set myfield = regexp_replace(myfield, '[^\w]+','');
    

    Which means that everything that is not a digit or a letter or an underline will be replaced by nothing (that includes -, space, dot, comma, etc).

    If you want to also include the _ to be replaced (\w will leave it) you can change the regex to [^\w]+|_.

    Or if you want to be strict with the characters that must be removed you use: [- ]+ in this case here a dash and a space.

    0 讨论(0)
提交回复
热议问题