【问题标题】:How to update a columns having a specific string patter/sequence如何更新具有特定字符串模式/序列的列
【发布时间】:2018-11-27 15:21:21
【问题描述】:

我的表中有一个列具有类似 'A=xxx^B=xxx^C=xxx^D=xxx^' 的模式值,我需要将所有具有此模式的列更新为类似 'C=xxx^D=xxx^' 的模式,其中 x 是一个数字。

【问题讨论】:

    标签: regex plsql oracle11g


    【解决方案1】:

    这样的东西会有帮助吗? REGEXP_LIKE 返回满足条件的行,而普通的SUBSTR 返回所需的结果。

    SQL> with test (col) as
      2    (select 'A=123^B=123^C=123^D=123^'       from dual union
      3     select 'A=123^B=456^C=789^D=987^'       from dual union
      4     select 'A=333^C=333^D=333^'             from dual union
      5     select 'C=987^D=987^'                   from dual union
      6     select 'B=876^'                         from dual union
      7     select 'A=123^B=123^C=123^D=123^E=123^' from dual
      8    )
      9  select col,
     10    substr(col, instr(col, 'C')) result
     11  from test
     12  where regexp_like(col, '^A=\d+{3}\^B=\d+{3}\^C=\d+{3}\^D=\d+{3}\^$');
    
    COL                            RESULT
    ------------------------------ ------------------------------
    A=123^B=123^C=123^D=123^       C=123^D=123^
    A=123^B=456^C=789^D=987^       C=789^D=987^
    
    SQL>
    

    【讨论】:

      【解决方案2】:

      我设法想出了一个解决方案,因为我正在寻找从 'A=' 开始的模式,我使用 REGEXP_LIKE 来找到该特定模式。然后我使用 SUBSTR 从应该从第二个 '^' 字符开始的字符串中提取值。

      Update MYTABLE t set t.key = SUBSTR(t.key,INSTR(t.key,'^',1,2)+1) WHERE REGEXP_LIKE(t.key_ref,'^A=') and t.dno = 'xxxxx';
      

      【讨论】:

      • 你的这种模式与你在问题中描述的模式相去甚远。
      • 我的错,我想说的是,通过 A=xxx 可以有一个数字(可能超过 3 位)。但是通过 xxx 它给出了 3 位数字的概念。我会更新我的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 2017-03-25
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多