Oracle 建立 split 和 splitstr 函數,splitsplitstr

來源:互聯網
上載者:User

Oracle 建立 split 和 splitstr 函數,splitsplitstr

Sql語句最好依次執行建立

/************************************** * name:        split * author:      sean zhang. * date:        2012-09-03. * function:    返回字串被指定字元分割後的表類型。 * parameters:  p_list: 待分割的字串。                p_sep: 分隔字元,預設逗號,也可以指定字元或字串。 * example:     select * from users where u_id in (select column_value from table (split ('1,2')))                返回u_id為1和2的兩行資料。 **************************************//* 建立一個表類型 */create or replace type tabletype as table of varchar2(32676)/* 建立 split 函數 */create or replace function split (p_list clob, p_sep varchar2 := ',')   return tabletype   pipelinedis   l_idx    pls_integer;   v_list   varchar2 (32676) := p_list;begin   loop      l_idx   := instr (v_list, p_sep);      if l_idx > 0      then         pipe row (substr (v_list, 1, l_idx - 1));         v_list   := substr (v_list, l_idx + length (p_sep));      else         pipe row (v_list);         exit;      end if;   end loop;end;/************************************** * name:        splitstr * author:      sean zhang. * date:        2012-09-03. * function:    返回字串被指定字元分割後的指定節點字串。 * parameters:  str: 待分割的字串。                i: 返回第幾個節點。當i為0返回str中的所有字元,當i 超過可被分割的個數時返回空。                sep: 分隔字元,預設逗號,也可以指定字元或字串。當指定的分隔字元不存在於str中時返回sep中的字元。 * example:     select splitstr('abc,def', 1) as str from dual;  得到 abc                select splitstr('abc,def', 3) as str from dual;  得到 空 **************************************//* 建立 splitstr 函數 */create or replace function splitstr(str in clob,                                    i   in number := 0,                                    sep in varchar2 := ',') return varchar2 is  t_i     number;  t_count number;  t_str   varchar2(4000);begin  if i = 0 then    t_str := str;  elsif instr(str, sep) = 0 then    t_str := sep;  else    select count(*) into t_count from table(split(str, sep));      if i <= t_count then      select str        into t_str        from (select rownum as item, column_value as str                from table(split(str, sep)))       where item = i;    end if;  end if;  return t_str;end;

樣本: split(字串,標識)

select  split('a,b,c,e,d,f,g')  arrData  from  dual;

預設使用逗號分割,可以自訂修改,如:select split('X-rapido & Lemon','&') arrData from dual;


點開集合


預設使用逗號分割,可以自訂修改,如:select split('X-rapido & Lemon','&') arrData from dual;

樣本:splitstr(字串,擷取的節點下標,分隔字元)

select splitstr('X-rapido&Lemon&Jennifer', 1, '&') word from dual;  -- X-rapido
select splitstr('X-rapido&Lemon&Jennifer', 2, '&') word from dual;  -- Lemon
select splitstr('X-rapido&Lemon&Jennifer', 3, '&') word from dual;  -- Jennifer
select splitstr('X-rapido&Lemon&Jennifer', 4, '&') word from dual;  -- Null 字元串


http://www.cnblogs.com/enjoycode/archive/2012/11/10/oracle_split.html


相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.