oracle中使用自定义函数解析指定分隔符的字符串

版权声明:请尊重每个人的努力! https://blog.csdn.net/IndexMan/article/details/87370863

1.创建字符串表类型

create type tab_varchar is table of varchar2(2000);

2.创建管道函数

create or replace function get_split_str(p_str varchar2, p_sep varchar2 default ',')
return tab_varchar pipelined
/**
  作者:laoxu
  功能:字符串分隔管道函数
  p_str    源字符串
  p_sep 分隔符,默认为逗号
*/
is
l_idx int:=0;
v_list varchar2(4000) := p_str;
begin
 loop
   l_idx := instr(v_list,p_sep);
   
   --没找到分隔符
   if l_idx = 0 then
     pipe row(v_list);
     return;
   --分隔符是第一个字符
   elsif l_idx = 1 then
        v_list := substr(v_list,l_idx+length(p_sep));
   --分隔符是最后一个字符
   elsif l_idx = length(v_list) then
       pipe row(substr(v_list,1,l_idx-1));
       return;
   else
   --打印第一个逗号之前的字符,然后继续执行
     pipe row(substr(v_list,1,l_idx-1));
     v_list := substr(v_list,l_idx+length(p_sep));
   end if;
  end loop;
end;


3.测试

select column_value from table(get_split_str(',111,123,234,'));

 

猜你喜欢

转载自blog.csdn.net/IndexMan/article/details/87370863