Oracle: Replacing non-numeric chars in a string

前端 未结 2 1178
北恋
北恋 2020-12-03 05:09

I have a field in my database where users have saved free-form telephone numbers. As a result, the data has all sorts of different formatting:

  • (area) nnn-nnnn<
相关标签:
2条回答
  • 2020-12-03 05:37

    You can use REGEXP_REPLACE since Oracle 10:

    SELECT REGEXP_REPLACE('+34 (947) 123 456 ext. 2013', '[^0-9]+', '')
    FROM DUAL
    

    This example returns 349471234562013.

    Alternative syntaxes include:

    • POSIX character classes:

      '[^[:digit:]]+'
      
    • Perl-influenced extensions (since Oracle 11):

      '\D+'
      
    0 讨论(0)
  • 2020-12-03 05:37

    For older versions of Oracle that don't support regular expressions:

    select translate (phone_no,'0'||translate (phone_no,'x0123456789','x'),'0')
    from mytable;
    

    The inner translate gets all the non-digit characters from the phone number, and the outer translate then removes them from the phone number.

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