新博客http://www.cnblogs.com/zhjh256 欢迎访问
分类: Oracle
2008-01-06 21:50:44
将表中的字符串值分隔成多个字符串,其中以逗号作为分隔符。
create table t1 (col1 number, col2 varchar2(200));
insert into t1 values (1,'table,chairs,cushion,sofa');
insert into t1 values (2,'tea,coffee,milk,sugar');
commit;
SELECT keyvalue, substr(p.mystring, p.pos1, p.pos2 - p.pos1) element
FROM (SELECT keyvalue,
instr(t.mystring, ',', 1, o.pos) + 1 pos1,
decode(instr(t.mystring, ',', 1, o.pos + 1),
0,
length(t.mystring) + 1,
instr(t.mystring, ',', 1, o.pos + 1)) pos2,
lower(t.mystring) AS mystring
FROM (SELECT col1 AS keyvalue, col2 AS mystring FROM t1) t,
(SELECT LEVEL AS pos FROM dual CONNECT BY LEVEL < 100) o
WHERE instr(t.mystring, ',', 1, o.pos) > 0) p
ORDER BY keyvalue, p.pos1;
---------------------
1 1 chairs
2 1 cushion
3 1 sofa
4 2 coffee
5 2 milk
6 2 sugar