Decode360's Blog

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

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 ::  :: 管理 ::
            397 隨筆 :: 33 文章 :: 29 評論 :: 0 Trackbacks
          <2008年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          公告

          話到七分,酒至微醺,筆墨疏宕,言詞婉約,古樸殘破,含蓄醞籍,就是不完而美之最高境界。

          常用鏈接

          留言簿(13)

          隨筆分類(397)

          隨筆檔案(397)

          文章分類(33)

          新聞分類(15)

          收藏夾(74)

          Blog_List

          IT_Web

          My_Link

          最新隨筆

          最新評論

          字符串分割問題[轉]
          ?
          ?
          ??? 字符串分割是一個經常會用到的功能,無論用什么語言。在Oracle里,可以解決字符串分割的方法有很多種,如果寫Function,應該是可以可以寫出適用性非常高的函數的。不過現在什么都流行直接用SQL來解決,下面轉兩篇文章來學習一下:
          ?
          ?
          分割字符串問題
          ===========================================================
          作者: 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
          ?
          ?
          ?
          posted on 2008-09-30 23:43 decode360 閱讀(263) 評論(0)  編輯  收藏 所屬分類: 05.SQL
          主站蜘蛛池模板: 普定县| 巴楚县| 天祝| 青州市| 清涧县| 永嘉县| 鹰潭市| 广汉市| 保德县| 大英县| 嘉峪关市| 于都县| 登封市| 富源县| 高陵县| 措勤县| 巴里| 南阳市| 新建县| 象山县| 卓资县| 定兴县| 凌源市| 青铜峡市| 泽普县| 新余市| 台北市| 博罗县| 尉氏县| 榆林市| 界首市| 佛坪县| 龙海市| 玉龙| 永康市| 广昌县| 雷波县| 香格里拉县| 永登县| 前郭尔| 明星|