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

--创建表类型 
Sql代码  收藏代码
  1. create or replace type mytype as table of number;  
  2. --如果定义成varchar  
  3. --CREATE OR REPLACE type mytype as table of varchar2(4000);  


-- 将字符串分割成数组 
Sql代码  收藏代码
  1. function my_split(piv_str in varchar2, piv_delimiter in varchar2)  
  2.   --piv_str 为字符串,piv_delimiter 为分隔符  
  3.   return mytype is  
  4.   j        int := 0;  
  5.   i        int := 1;  
  6.   len      int := 0;  
  7.   len1     int := 0;  
  8.   str      varchar2(4000);  
  9.   my_split mytype := mytype();  
  10. begin  
  11.   len  := length(piv_str);  
  12.   len1 := length(piv_delimiter);  
  13.   while j < len loop  
  14.     j := instr(piv_str, piv_delimiter, i);  
  15.     if j = 0 then  
  16.       j   := len;  
  17.       str := substr(piv_str, i);  
  18.       my_split.extend;  
  19.       my_split(my_split.count) := str;  
  20.       if i >= len then  
  21.         exit;  
  22.       end if;  
  23.     else  
  24.       str := substr(piv_str, i, j - i);  
  25.       i   := j + len1;  
  26.       my_split.extend;  
  27.       my_split(my_split.count) := str;  
  28.     end if;  
  29.   end loop;  
  30.   return my_split;  
  31. end my_split;  

  
-- 函数调用,两个参数:字符串和分隔符 
Sql代码  收藏代码
  1. select  column_value  from table(my_split('7369,7499,7521,7844',','));  



-- 输出结果如下 
7369 
7499 
7521 
7844
 
原文地址:https://www.cnblogs.com/likeju/p/5016119.html