例如,有如下一张表,表名为bk_test。插入了以下数据:
CREATE TABLE BK_TESK(id varchar2(10),s varchar2(20));
insert into BK_TESK values ('A','1,2,3');
insert into BK_TESK values ('B','4,5,6');
insert into BK_TESK values ('C','7,8');
查询数据如图所示
我们需要将字段S中以逗号分隔的数据拆分成多行。结果如图所示
实现将一行数据拆分成多行的代码如下所示
SELECT
id,
TRIM(REGEXP_SUBSTR(s, '[^,]+', 1, LEVEL)) AS s
FROM
BK_TESK
CONNECT BY
PRIOR SYS_GUid() IS NOT NULL
AND LEVEL <= REGEXP_COUNT(s, ',') + 1
AND PRIOR id = id
ORDER BY
id, s;