【问题标题】:REGEXP_REPLACE in OracleOracle 中的 REGEXP_REPLACE
【发布时间】:2019-10-20 02:11:18
【问题描述】:

我需要使用 REGEXP_REPLACE 来执行以下操作:

 If word starts with 'ABCD' then replace first four(4) chars with 'FFFF'
    else
 If word starts with 'XYZ' then replace first three(3) chars with 'GGG'

如何使用 REGEXP_REPLACE 进行条件替换?

【问题讨论】:

  • 使用 REGEXP_REPLACE 是问题陈述的一部分,还是您想要提供的解决方案的一部分?最好的答案(由 Gordon Linoff 给出)不使用它。

标签: sql oracle regexp-replace


【解决方案1】:

你可以使用case和字符串操作:

select (case when word like 'ABCD%'
             then 'FFFF' || substr(word, 5)
             when word like 'XYZ%'
             then 'GGG' || substr(word, 4)
             else word
        end) as new_word

【讨论】:

    【解决方案2】:

    如果必须是REGEXP_REPLACE,则必须合并两个函数调用:

    REGEXP_REPLACE( 
      REGEXP_REPLACE(word,'^ABCD','FFFF')
      ,'^XYZ', 'GGG')
    

    但我更喜欢 Gordon 的 caseapproach...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-20
      • 2014-05-28
      • 2018-10-07
      • 2018-04-23
      • 1970-01-01
      • 2013-04-10
      • 2018-07-20
      • 2016-06-13
      相关资源
      最近更新 更多