SELECT l_count, REGEXP_SUBSTR('add, daddf, dsdf, asdfa, dsfasd, dsfad','[^,]+',1,l_count) AS NAME
FROM dual
,(SELECT LEVEL l_count FROM DUAL CONNECT BY LEVEL<=100)
WHERE l_count <=LENGTH('add, daddf, dsdf, asdfa, dsfasd, dsfad') - LENGTH(REPLACE('add, daddf, dsdf, asdfa, dsfasd, dsfad',','))+1
查询结果为:
lcount name
1 add
2 daddf
3 dsdf
4 asdfa
5 dsfasd
6 dsfad
一种奇特的字符串拆分方法
create table t_test (id number, names varchar2(200));
insert into t_test values (1,'a1,a2,a3,a4');
insert into t_test values (2,'b1,b2,b3');
insert into t_test values (3,'c1,c2,c3,c4,c5');
目标输出:
ID NAME
--- ----
1 a1
1 a2
1 a3
1 a4
2 b1
2 b2
2 b3
3 c1
3 c2
3 c3
3 c4
3 c5
常规做法:
SELECT id
,REGEXP_SUBSTR(names,'[^,]+',1,l) AS NAME
FROM t_test
,(SELECT LEVEL l FROM DUAL CONNECT BY LEVEL<=100)
WHERE l <=LENGTH(names) - LENGTH(REPLACE(names,','))+1
ORDER BY 1,2;
下面这种写法(从ASKTOM看来的),很奇怪地在T2中可以看到T1的列(如果不用TABLE,CAST,MULTISET就不行), T1和T2两个集合之间没有任何连接条件, 看起来像Correlated Subquery, 但它是写在from的地方:
SELECT id
,column_value
FROM (SELECT id,','||names||',' names FROM t_test) t1 ----- 前后拼上逗号是为了下面定位拆分
,TABLE(CAST(MULTISET( SELECT SUBSTR (names ----- 此处竟然可以看到t1.names
,INSTR (names, ',', 1, LEVEL ) + 1
,INSTR (names, ',', 1, LEVEL+1) - INSTR (names, ',', 1, LEVEL) -1
)
FROM DUAL
CONNECT BY LEVEL <= LENGTH(names)-LENGTH(REPLACE(names,',',''))-1
)
AS SYS.ODCIVARCHAR2LIST ) ------ SYS.ODCIVARCHAR2LIST 可以换成任意一个TABLE OF VARCHAR2的嵌套表类型
) t2
ORDER BY 1,2;
后边一种很奇怪,先存起来,有空再来研究吧