Oracle 创建 split 和 splitstr 函数 您所在的位置:网站首页 oraclesplit函数用法 Oracle 创建 split 和 splitstr 函数

Oracle 创建 split 和 splitstr 函数

2023-08-10 17:39| 来源: 网络整理| 查看: 265

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  pipelined

is  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



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有