ORACLE 中字符串分割函数(转载)

oracle根据分隔符将字符串分割成数组函数

2013年01月16日 10:29:32 liangjie5305579 阅读数:43

--创建表类型

create or replace type mytype as table of number;
--如果定义成varchar
--CREATE OR REPLACE type mytype as table of varchar2(4000);



-- 将字符串分割成数组

function my_split(piv_str in varchar2, piv_delimiter in varchar2)
  --piv_str 为字符串,piv_delimiter 为分隔符
  return mytype is
  j        int := 0;
  i        int := 1;
  len      int := 0;
  len1     int := 0;
  str      varchar2(4000);
  my_split mytype := mytype();
begin
  len  := length(piv_str);
  len1 := length(piv_delimiter);
  while j < len loop
    j := instr(piv_str, piv_delimiter, i);
    if j = 0 then
      j   := len;
      str := substr(piv_str, i);
      my_split.extend;
      my_split(my_split.count) := str;
      if i >= len then
        exit;
      end if;
    else
      str := substr(piv_str, i, j - i);
      i   := j + len1;
      my_split.extend;
      my_split(my_split.count) := str;
    end if;
  end loop;
  return my_split;
end my_split;



-- 函数调用,两个参数:字符串和分隔符

select  column_value  from table(my_split('7369,7499,7521,7844',','));




-- 输出结果如下
7369
7499
7521
7844

————————————————————————————————————————————————————

下面代码我已经验证好用

CREATE OR REPLACE 
function my_split(piv_str in varchar2, piv_delimiter in varchar2)
    --piv_str 为字符串,piv_delimiter 为分隔符
    return split_type is
    j        int := 0;
    i        int := 1;
    len      int := 0;
    len1     int := 0;
    str      varchar2(4000);
    my_split split_type := split_type();
  begin
    len  := length(piv_str);
    len1 := length(piv_delimiter);
    while j < len loop
      j := instr(piv_str, piv_delimiter, i);
      if j = 0 then
        j   := len;
        str := substr(piv_str, i);
        my_split.extend;
        my_split(my_split.count) := str;
        if i >= len then
          exit;
        end if;
      else
        str := substr(piv_str, i, j - i);
        i   := j + len1;
        my_split.extend;
        my_split(my_split.count) := str;
      end if;
    end loop;
    return my_split;
  end my_split;
 环境下截图:
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值