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

How to remove duplicates from comma separated list by regex in Oracle but I don't want duplicates values?

我有这个字符串

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',
     '([^,]+)(,)+', ''
 ) test
from dual;

试试这个,根据文章 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;

Fiddle: http://sqlfiddle.com/#!4/c858d/5

这是一种选择:

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>