【问题标题】:How to special character after 3rd character in Oracle SQL query如何在 Oracle SQL 查询中的第三个字符之后使用特殊字符
【发布时间】:2021-09-13 07:35:17
【问题描述】:

我想在第三个字符之后添加特殊字符。下面是例子

Current Phone Number:- 123234567
Expected output     :- (123)234-567

我该怎么做?

【问题讨论】:

    标签: sql oracle special-characters string-concatenation


    【解决方案1】:

    如您所说,连接以及substr 函数:

    SQL> with test (col) as
      2    (select 123234567 from dual)
      3  select '(' || substr(col, 1, 3) || ')' ||substr(col, 4, 3) ||'-'|| substr(col, 7) result
      4  from test;
    
    RESULT
    ------------
    (123)234-567
    
    SQL>
    

    或者,使用正则表达式:

    SQL> with test (col) as
      2    (select 123234567 from dual)
      3  select regexp_replace(col, '([0-9]{3})([0-9]{3})', '(\1)\2-') result_2
      4  from test;
    
    RESULT_2
    ------------
    (123)234-567
    
    SQL>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 2012-08-23
      • 2020-04-26
      • 2020-09-28
      • 2023-04-04
      相关资源
      最近更新 更多