How to convert comma separated values to rows in oracle?

前端 未结 4 1398
死守一世寂寞
死守一世寂寞 2020-11-22 07:55

Here is the DDL --

create table tbl1 (
   id number,
   value varchar2(50)
);

insert into tbl1 values (1, \'AA, UT, BT, SK, SX\');
insert into tbl1 values (         


        
4条回答
  •  一个人的身影
    2020-11-22 08:28

    Vercelli posted a correct answer. However, with more than one string to split, connect by will generate an exponentially-growing number of rows, with many, many duplicates. (Just try the query without distinct.) This will destroy performance on data of non-trivial size.

    One common way to overcome this problem is to use a prior condition and an additional check to avoid cycles in the hierarchy. Like so:

    select id, trim(regexp_substr(value,'[^,]+', 1, level) ) value, level
      from tbl1
       connect by regexp_substr(value, '[^,]+', 1, level) is not null
              and prior id = id
              and prior sys_guid() is not null
       order by id, level;
    

    See, for example, this discussion on OTN: https://community.oracle.com/thread/2526535

提交回复
热议问题