如何在Oracle中使用正则表达式从逗号分隔列表中删除重复项,但我不想要重复值?

时间:2018-02-27 12:52:59

标签: sql regex oracle regexp-replace

我有这个字符串

ABCD1234, XYZ, ABCD1234, ABCD1234C, ABCD1234, abc, abcX, 1234U, 1234

我想要,但我不想要重复

ABCD1234, XYZ, ABCD1234C, abc, abcX, 1234U, 1234,

我正在使用以下正则表达式

select regexp_replace (
    'ABCD1234, XYZ, ABCD1234, ABCD1234C, ABCD1234, abc, abcX, 1234U, 1234',
     '([^,]+)(,\1)+', '\1'
 ) test
from dual;

2 个答案:

答案 0 :(得分:0)

根据文章http://www.dba-oracle.com/t_extract_comma_delimited_strings_oracle_sql.html尝试此操作:

select distinct str from
(select regexp_substr ('ABCD1234, XYZ, ABCD1234, ABCD1234C, ABCD1234, abc, abcX, 1234U, 1234', '[^, ]+',1, rownum) str 
from dual 
connect by level <= regexp_count ('ABCD1234, XYZ, ABCD1234, ABCD1234C, ABCD1234, abc, abcX, 1234U, 1234', '[^, ]+')) v;

小提琴:http://sqlfiddle.com/#!4/c858d/5

答案 1 :(得分:0)

这是一个选项:

SQL> with test (id, col) as
  2    (select 1, 'ABCD1234, XYZ, ABCD1234, ABCD1234C, ABCD1234, abc, abcX, 1234U, 1234' from dual union
  3     select 2, '111, 222, 111, ABCX2, FFE, 222' from dual
  4    ),
  5  t_rows as
  6    (select id, regexp_substr(col, '[^, ]+', 1, column_value) col
  7     from test,
  8          table(cast(multiset(select level from dual
  9                              connect by level <= regexp_count(col, ',') + 1)
 10                              as sys.odcinumberlist))
 11    ),
 12  t_distinct as
 13    (select distinct id, col
 14     from t_rows
 15    )
 16  select id, listagg(col, ', ') within group (order by null) col
 17  from t_distinct
 18  group by id;

        ID COL
---------- --------------------------------------------------
         1 1234, 1234U, ABCD1234, ABCD1234C, XYZ, abc, abcX
         2 111, 222, ABCX2, FFE

SQL>