1

我的数据库中有列,其中的值如下所示:

3862,3654,3828

在虚拟列中任何没有。逗号分隔的值可以来。我尝试使用以下查询,但它正在创建重复的结果。

select regexp_substr(dummy,'[^,]+',1,Level) as dummycol 
  from (select * from dummy_table) 
 connect by level <= length(REGEXP_REPLACE(dummy,'[^,]+'))+1

我不明白这个问题。任何人都可以帮忙吗?

4

2 回答 2

3

非常适合我 -

SQL> WITH dummy_table AS(
  2  SELECT '3862,3654,3828' dummy FROM dual
  3  )
  4  SELECT trim(regexp_substr(dummy,'[^,]+',1,Level)) AS dummycol
  5  FROM dummy_table
  6    CONNECT BY level <= LENGTH(REGEXP_REPLACE(dummy,'[^,]+'))+1
  7  /

DUMMYCOL
--------------
3862
3654
3828

SQL>

还有许多其他方法可以实现它。读取将单个逗号分隔的字符串拆分为行

更新关于使用列而不是单个字符串值时的重复项。看到这里使用了 PRIOR 子句中的 DBMS_RANDOM 来摆脱循环循环

试试下面的,

SQL> WITH dummy_table AS
  2    ( SELECT 1 rn, '3862,3654,3828' dummy FROM dual
  3    UNION ALL
  4    SELECT 2, '1234,5678' dummy FROM dual
  5    )
  6  SELECT trim(regexp_substr(dummy,'[^,]+',1,Level)) AS dummycol
  7  FROM dummy_table
  8    CONNECT BY LEVEL          <= LENGTH(REGEXP_REPLACE(dummy,'[^,]+'))+1
  9  AND prior rn                 = rn
 10  AND PRIOR DBMS_RANDOM.VALUE IS NOT NULL
 11  /

DUMMYCOL
--------------
3862
3654
3828
1234
5678

SQL>

更新 2

另一种方式,

SQL> WITH dummy_table AS
  2    ( SELECT 1 rn, '3862,3654,3828' dummy FROM dual
  3    UNION ALL
  4    SELECT 2, '1234,5678,xyz' dummy FROM dual
  5    )
  6  SELECT trim(regexp_substr(t.dummy, '[^,]+', 1, levels.column_value)) AS dummycol
  7  FROM dummy_table t,
  8    TABLE(CAST(MULTISET
  9    (SELECT LEVEL
 10    FROM dual
 11      CONNECT BY LEVEL <= LENGTH (regexp_replace(t.dummy, '[^,]+')) + 1
 12    ) AS sys.OdciNumberList)) LEVELS
 13    /

DUMMYCOL
--------------
3862
3654
3828
1234
5678
xyz

6 rows selected.

SQL>
于 2015-02-02T05:52:28.633 回答
0

给出一个 PL/SQL 示例,其中解析具有 ID 和列名的表。这将解析并打印出每个 ID 和解析值,然后可以将其插入新表或以其他方式使用。

输入

Column_ID    Column_Name
123          (3862,3654,3828)

输出

Column_ID    Column_Name
123          3862
123          3654
123          3828

PL/SQL 代码

declare
   table_name1 varchar2(1000);
   string_to_parse varchar2(2000); -- assign string to table name
   string_length   number := 0;    -- string length for loop
   string_value    varchar2(2000); -- string value to store value in
   column_id        number; 
begin
  --some table in the format '123' as column_id, '(3862,3654,3828)' as column_name

  --remove the parenthesis or other special characters if needed
  update some_table t
     set t.column_name = regexp_replace(t.column_name,'\(|\)','');
  commit;

  for i in (
    select * from some_table
    ) loop

    column_id := i.column_id; --assign the id of the colors
    string_to_parse := i.column_name; -- assign string to be parsed

    if string_to_parse is null then
      --at this point insert into a new table, or do whatever else you need
      dbms_output.put_line(column_id || ' ' || string_value);
    else
      --String to parse is the comma
      string_to_parse := string_to_parse||',';
      string_length   := length(string_to_parse) - length(replace(string_to_parse,',',''));

      -- Loop through string from parameter
      for i in 1 .. string_length loop
        -- [^,] matches any character except for the ,
        select regexp_substr(string_to_parse,'[^,]+',1,i)
        into string_value -- stores value into string_value
        from dual; -- dual is a dummy table to work around

        --at this point insert into a new table, or do whatever else you need
        dbms_output.put_line(column_id || ' ' || string_value);
        --clear out the string value
        string_value := null;
      end loop;
    end if;
  end loop;
end;
于 2015-12-02T14:22:05.997 回答