Decode360's Blog

业精于勤而荒于嬉 QQ:150355677 MSN:decode360@hotmail.com

  BlogJava :: 首页 :: 新随笔 :: 联系 ::  :: 管理 ::
  302 随笔 :: 26 文章 :: 82 评论 :: 0 Trackbacks
分割字符串问题
===========================================================
作者: 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,'
百思不得其解,是高手的试一下。
 
解答:
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 (根据分割后的字符串排序, 不是子串在原字符串中的顺序)
 
解答:
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,','))
 
 
 
 
 
 
分割串问题
===========================================================
作者: 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@@
 
SQL> create or replace type t_object as object(
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 /
 
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
 

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
 
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




-The End-

posted on 2008-09-30 13:43 decode360-3 阅读(351) 评论(0)  编辑  收藏 所属分类: SQL Dev

只有注册用户登录后才能发表评论。


网站导航: