使用正则表达式查找并追加到列中

时间:2019-03-29 07:51:02

标签: sql oracle

我有一个名为test的表。我需要找到任何字母,并使用正则表达式在名称栏中添加ALL。请输入您的意见。

注意:将来的值也将插入到该列中。因此,在运行时应将其替换。

create table test(id number, name varchar2(10));
insert INTO TEST VALUES (1,'_A');
insert INTO TEST VALUES (2,'_F');
insert INTO TEST VALUES (3,'_K');
insert INTO TEST VALUES (4,'_B || _G');

我的输出应如下所示

-------------------------------
  id    column
--------------------------------
    1   _AALL
    2   _FALL
    3   _KALL
    4   _BALL || _GALL

2 个答案:

答案 0 :(得分:0)

您可以使用regexp_replace

with test as(
  select 1 as id , '_A' as l from dual union all
  select 1 as id , '_B || _G' as l from dual 
)

select t.*
      , trim(regexp_replace(t.l,'(_[A-Z])( |$)','\1'||'ALL '))
  from test t

答案 1 :(得分:0)

with test as(
  select 1 as id , '_A' as l from dual union all
  select 1 as id , '_B || _G' as l from dual 
)
select t.*,
regexp_replace(t.l,'([A-Z])','\1'||'ALL') rps
from test t;

        ID L                    RPS                 
---------- -------------------- --------------------
         1 _A                   _AALL               
         1 _B || _G             _BALL || _GALL