念写一个split函数拆分字符串,不知道能否实现
想写一个split函数拆分字符串,不知道能否实现
库表结构如下:
create table demotab(oid number, namelist varchar2(5000));
里面数据格式如下:
oid|namelist
1|a,b,c
2|d,e,f
...许多行
============================
希望写一个函数,能返回以下两列
oid, name
1, a
1, b
1, c
2, d
2, e
2, f
...许多行
===========================
不知道是否可行,网上有一个split函数(http://www.cnblogs.com/linbaoji/archive/2009/09/17/1568252.html),只能处理固定的字符串,而且只能返回一列
无法两列多行
------解决方案--------------------
你可以先把 namelist 列拆分成很多列,然后再列转行,这个有很多例子可以搜索借鉴的
------解决方案--------------------
方法有很多,给你写一种通常的写法吧!
库表结构如下:
create table demotab(oid number, namelist varchar2(5000));
里面数据格式如下:
oid|namelist
1|a,b,c
2|d,e,f
...许多行
============================
希望写一个函数,能返回以下两列
oid, name
1, a
1, b
1, c
2, d
2, e
2, f
...许多行
===========================
不知道是否可行,网上有一个split函数(http://www.cnblogs.com/linbaoji/archive/2009/09/17/1568252.html),只能处理固定的字符串,而且只能返回一列
无法两列多行
------解决方案--------------------
你可以先把 namelist 列拆分成很多列,然后再列转行,这个有很多例子可以搜索借鉴的
------解决方案--------------------
方法有很多,给你写一种通常的写法吧!
- SQL code
SQL> with tmp as 2 ( 3 select '1|a,b,c' str from dual union all 4 select '2|d,e,f' str from dual 5 ) 6 select distinct id, regexp_substr(name,'[^,]+',1,level) name 7 from (select substr(str,1,instr(str,'|')-1) id, 8 substr(str,instr(str,'|')+1) name 9 from tmp 10 ) 11 connect by level<=length(name)-length(replace(name,',',''))+1 12 order by id, name; ID NAME -------------- -------------- 1 a 1 b 1 c 2 d 2 e 2 f 6 rows selected