zoukankan      html  css  js  c++  java
  • Oracle-函数-split 和 splitstr 的创建

     1 /**************************************  
     2  * name:        split  
     3  * author:      sean zhang.  
     4  * date:        2012-09-03.  
     5  * function:    返回字符串被指定字符分割后的表类型。  
     6  * parameters:  p_list: 待分割的字符串。  
     7                 p_sep: 分隔符,默认逗号,也可以指定字符或字符串。  
     8  * example:     select * from users where u_id in (select column_value from table (split ('1,2')))  
     9                 返回u_id为1和2的两行数据。  
    10  **************************************/  
    11 /* 创建一个表类型 */  
    12 create or replace type tabletype as table of varchar2(32676)  
    13   
    14 /* 创建 split 函数 */  
    15 create or replace function split (p_list clob, p_sep varchar2 := ',')  
    16    return tabletype  
    17    pipelined  
    18   
    19 is  
    20    l_idx    pls_integer;  
    21    v_list   varchar2 (32676) := p_list;  
    22 begin  
    23    loop  
    24       l_idx   := instr (v_list, p_sep);  
    25   
    26       if l_idx > 0  
    27       then  
    28          pipe row (substr (v_list, 1, l_idx - 1));  
    29          v_list   := substr (v_list, l_idx + length (p_sep));  
    30       else  
    31          pipe row (v_list);  
    32          exit;  
    33       end if;  
    34    end loop;  
    35 end;  
    36   
    37   
    38   
    39 /**************************************  
    40  * name:        splitstr  
    41  * author:      sean zhang.  
    42  * date:        2012-09-03.  
    43  * function:    返回字符串被指定字符分割后的指定节点字符串。  
    44  * parameters:  str: 待分割的字符串。  
    45                 i: 返回第几个节点。当i为0返回str中的所有字符,当i 超过可被分割的个数时返回空。  
    46                 sep: 分隔符,默认逗号,也可以指定字符或字符串。当指定的分隔符不存在于str中时返回sep中的字符。  
    47  * example:     select splitstr('abc,def', 1) as str from dual;  得到 abc  
    48                 select splitstr('abc,def', 3) as str from dual;  得到 空  
    49  **************************************/  
    50 /* 创建 splitstr 函数 */  
    51 create or replace function splitstr(str in clob,  
    52                                     i   in number := 0,  
    53                                     sep in varchar2 := ',') return varchar2 is  
    54   t_i     number;  
    55   t_count number;  
    56   t_str   varchar2(4000);  
    57 begin  
    58   if i = 0 then  
    59     t_str := str;  
    60   elsif instr(str, sep) = 0 then  
    61     t_str := sep;  
    62   else  
    63     select count(*) into t_count from table(split(str, sep));  
    64     
    65     if i <= t_count then  
    66       select str  
    67         into t_str  
    68         from (select rownum as item, column_value as str  
    69                 from table(split(str, sep)))  
    70        where item = i;  
    71     end if;  
    72   end if;  
    73   
    74   return t_str;  
    75 end;

    示例:split(字符串,标识)

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

    默认使用逗号分割,可以自定义修改,如:select split('X-rapido & Lemon','&') arrData from dual;

    Oracle 创建 split 和 splitstr 函数

    点开集合

    Oracle 创建 split 和 splitstr 函数

    默认使用逗号分割,可以自定义修改,如:select split('X-rapido & Lemon','&') arrData from dual;

    示例:splitstr(字符串,获取的节点下标,分隔符)

    1 select splitstr('X-rapido&Lemon&Jennifer', 1, '&') word from dual;  -- X-rapido
    2 select splitstr('X-rapido&Lemon&Jennifer', 2, '&') word from dual;  -- Lemon
    3 select splitstr('X-rapido&Lemon&Jennifer', 3, '&') word from dual;  -- Jennifer
    4 select splitstr('X-rapido&Lemon&Jennifer', 4, '&') word from dual;  -- 空字符串
  • 相关阅读:
    E
    牛客比赛—身体训练
    前缀和例题
    欧拉函数模板
    3.30训练题
    poj1321棋盘问题
    记set学习
    B. K-th Beautiful String
    codeforces1293C
    LightOJ 1370 Bi-shoe and Phi-shoe
  • 原文地址:https://www.cnblogs.com/bishuihengchen/p/8406130.html
Copyright © 2011-2022 走看看