簡體   English   中英

Oracle中的REGEXP_REPLACE

[英]REGEXP_REPLACE in Oracle

我需要使用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進行條件替換?

您可以使用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

如果必須是REGEXP_REPLACE ,則必須組合兩個函數調用:

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

但我會優先考慮戈登的case方法......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM