字符串分割问题[转]
字符串分割问题[转]
字符串分割是一个经常会用到的功能,无论用什么语言。在Oracle里,可以解决字符串分割的方法有很多种,如果写Function,应该是可以可以写出适用性非常高的函数的。不过现在什么都流行直接用SQL来解决,下面转两篇文章来学习一下:
分割字符串问题
===========================================================
作者: zhouwf0726( http://zhouwf0726.itpub.net )
发表于:2006.09.06 12:58
分类: oracle开发
出处: http://zhouwf0726.itpub.net/post/9689/203877
---------------------------------------------------------------
作者: zhouwf0726( http://zhouwf0726.itpub.net )
发表于:2006.09.06 12:58
分类: oracle开发
出处: http://zhouwf0726.itpub.net/post/9689/203877
---------------------------------------------------------------
问题源自 http://www.itpub.net/626418.html
怎样支掉字符串中逗号间重复的字符
如 ',1,2,5,9,1,2,5,9,1,2,9,1,2,9,1,2,3,9,1,2,3,9,1,2,9,1,2,9,1,2,3,9,1,2,3,9,'怎样支掉字符串中逗号间重复的字符,并将字符升序排列,得到
',1,2,3,5,9,'
百思不得其解,是高手的试一下。
如 ',1,2,5,9,1,2,5,9,1,2,9,1,2,9,1,2,3,9,1,2,3,9,1,2,9,1,2,9,1,2,3,9,1,2,3,9,'怎样支掉字符串中逗号间重复的字符,并将字符升序排列,得到
',1,2,3,5,9,'
百思不得其解,是高手的试一下。
解答:
select col from(
select sys_connect_by_path(col,',')||',' col,level from(
select col,row_number() over(order by rownum) rn from (
select distinct substr(col,instr(col,',',1,rownum)+1,instr(col,',',1,rownum+1)-instr(col,',',1,rownum)-1) col from (
select ',1,2,5,9,1,2,5,9,1,3,9,' col from dual
) connect by rownum<length(translate(col,','||col,','))
)
)
connect by prior rn = rn -1 order by level desc
) where rownum=1
select col from(
select sys_connect_by_path(col,',')||',' col,level from(
select col,row_number() over(order by rownum) rn from (
select distinct substr(col,instr(col,',',1,rownum)+1,instr(col,',',1,rownum+1)-instr(col,',',1,rownum)-1) col from (
select ',1,2,5,9,1,2,5,9,1,3,9,' col from dual
) connect by rownum<length(translate(col,','||col,','))
)
)
connect by prior rn = rn -1 order by level desc
) where rownum=1
这个问题的解决办法中的一部分(按照固定分隔符分割字符串)可以解决 http://www.itpub.net/515354.html
要求用pl/sql写一个函数, 实现根据分割符把原字符串分成若干个字符串功能.
输入: string(字符串) 和 Delimiter (分隔符)
输出: substr1, ..., substrn (根据分割后的字符串排序, 不是子串在原字符串中的顺序)
输出: substr1, ..., substrn (根据分割后的字符串排序, 不是子串在原字符串中的顺序)
解答:
select substr(col,instr(col,',',1,rownum)+1,instr(col,',',1,rownum+1)-instr(col,',',1,rownum)-1) col from (
select ',1,2,5,9,1,2,5,9,1,3,9,' col from dual
) connect by rownum<length(translate(col,','||col,','))
select ',1,2,5,9,1,2,5,9,1,3,9,' col from dual
) connect by rownum<length(translate(col,','||col,','))
分割串问题
===========================================================
作者: zhouwf0726( http://zhouwf0726.itpub.net )
发表于:2007.03.08 17:31
分类: oracle开发
出处: http://zhouwf0726.itpub.net/post/9689/269709
---------------------------------------------------------------
作者: zhouwf0726( http://zhouwf0726.itpub.net )
发表于:2007.03.08 17:31
分类: oracle开发
出处: http://zhouwf0726.itpub.net/post/9689/269709
---------------------------------------------------------------
一个网友的问题解决记录 http://www.itpub.net/showthread.php?s=&threadid=723791
我现在表有个字段是ids并且以@@分割,例如@@123@@234@@567@@.
现在有一个select id from project查出来的结果集(如查出来id是123,234,555)现在我想用like匹配这个结果集,只要@@123@@234@@567@@.有一个id匹配出来出的结果集就OK
SQL> select * from tt;
ID
------------------------------------------------------------
@@aa@@bb@@cc@@
@@aaa@@bbb@@ccc@@
------------------------------------------------------------
@@aa@@bb@@cc@@
@@aaa@@bbb@@ccc@@
SQL> create or replace type t_object as object(
2 id varchar2(60),
3 sub_id varchar2(60)
4 );
2 id varchar2(60),
3 sub_id varchar2(60)
4 );
Type created
SQL> create type t_ret_table is table of t_object;
Type created
SQL> create or replace function f_test(var_str in varchar2) return t_ret_table PIPELINED
2 as
3 var_tmp varchar2(60);
4 var_element varchar2(60);
5 begin
6 for i in (select rtrim(ltrim(id,'@@'),'@@') id from tt) loop
7 var_tmp := i.id;
8 while instr(var_tmp,'@@')>0 loop
9 var_element := substr(var_tmp,1,instr(i.id,'@@')-1);
10 var_tmp := substr(var_tmp,instr(i.id,'@@')+2,length(var_tmp));
11 pipe row(t_object(i.id,var_element));
12 end loop;
13 pipe row(t_object(i.id,var_tmp));
14 end loop;
15 return;
16 end f_test;
17 /
2 as
3 var_tmp varchar2(60);
4 var_element varchar2(60);
5 begin
6 for i in (select rtrim(ltrim(id,'@@'),'@@') id from tt) loop
7 var_tmp := i.id;
8 while instr(var_tmp,'@@')>0 loop
9 var_element := substr(var_tmp,1,instr(i.id,'@@')-1);
10 var_tmp := substr(var_tmp,instr(i.id,'@@')+2,length(var_tmp));
11 pipe row(t_object(i.id,var_element));
12 end loop;
13 pipe row(t_object(i.id,var_tmp));
14 end loop;
15 return;
16 end f_test;
17 /
Function created
SQL> select id from table(f_test('a')) where sub_id in (select col from (select 'aa' col from dual union select 'bbb' col from dual union select 'ccc' from dual)) group by id;
ID
------------------------------------------------------------
aa@@bb@@cc
aaa@@bbb@@ccc
------------------------------------------------------------
aa@@bb@@cc
aaa@@bbb@@ccc
SQL>select * from table(f_test('a'));
ID
SUB_ID
-------------------------------------- ---------------------
aa@@bb@@cc aa
aa@@bb@@cc bb
aa@@bb@@cc cc
aaa@@bbb@@ccc aaa
aaa@@bbb@@ccc bbb
aaa@@bbb@@ccc ccc
-------------------------------------- ---------------------
aa@@bb@@cc aa
aa@@bb@@cc bb
aa@@bb@@cc cc
aaa@@bbb@@ccc aaa
aaa@@bbb@@ccc bbb
aaa@@bbb@@ccc ccc
6 rows selected.
SQL>select id from table(f_test('a')) where sub_id in (
select substr(col,instr(col,',',1,rownum)+1,instr(col,',',1,rownum+1)-instr(col,',',1,rownum)-1) col from (
select ','||'aa,aaa,bbb'||',' col from dual
) connect by rownum<length(translate(col,','||col,','))
)
group by id
select substr(col,instr(col,',',1,rownum)+1,instr(col,',',1,rownum+1)-instr(col,',',1,rownum)-1) col from (
select ','||'aa,aaa,bbb'||',' col from dual
) connect by rownum<length(translate(col,','||col,','))
)
group by id