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 閱讀(365) 評論(0)  編輯  收藏 所屬分類: SQL Dev
          主站蜘蛛池模板: 乐陵市| 百色市| 甘孜县| 增城市| 当阳市| 镇原县| 太康县| 乐东| 天长市| 阜康市| 无极县| 宾川县| 鞍山市| 新宁县| 陕西省| 英超| 手游| 城步| 和硕县| 泾源县| 日喀则市| 疏勒县| 桂东县| 十堰市| 涟源市| 德安县| 太谷县| 台北县| 韶山市| 潜山县| 石楼县| 钦州市| 南平市| 天全县| 夏河县| 卢龙县| 高邑县| 灵寿县| 建平县| 饶平县| 霍城县|