1.函数可以将“目标字符串”以“指定字符串”进行拆分,并通过表结构返回结果。代码如下:

create or replace function FUNC_SPLIT(p_list varchar2,
                                      p_sep  varchar2 in varchar2)
  return type_split
  pipelined is
  l_idx  pls_integer;
  v_list varchar2(4000) := 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;
  return;
end FUNC_SPLIT;

如果字符串是逗号隔开的,参数p_sep可以默认写成:p_sep varchar2 := ',';

2.创建返回类型

CREATE OR REPLACE TYPE "TYPE_SPLIT"   as table of varchar2(4000)

3.使用说明

select * from table(func_split('Hello,split!',','));

效果图:

select a.column_value v1,b.column_value v2 from 
(select * from (select rownum rn,t.* from table(func_split('Hello,Cyrus Joyce',',')) t)) a,
(select * from (select rownum rn,t.* from table(func_split('Hello,Cyrus Joyce',',')) t)) b
where a.rn=1 and b.rn=2

效果图: