在oracle中,实现如下功能:
把'a|||b|||c'字符串分隔成
a
b
c
sql代码:
--分隔字符串
create or replace function f_test(var_str in string) return t_ret_table is
var_out t_ret_table;
var_tmp varchar2(2000);
var_element varchar2(2000);
begin
var_tmp := var_str;
var_out := t_ret_table();
while instr(var_tmp,'|||')>0 loop
var_element := substr(var_tmp,1,instr(var_str,'|||')-1);
var_tmp := substr(var_tmp,instr(var_str,'|||')+3,length(var_tmp));
var_out.extend(1);
var_out(var_out.count) := var_element;
end loop;
var_out.extend(1);
var_out(var_out.count) := var_tmp;
return var_out;
end f_test;
/
set serverout on
declare
aa t_ret_table;
--开如字符串的分隔
begin
aa := f_test('a|||b|||c');
for i in 1..aa.count loop
dbms_output.put_line(aa(i));
end loop;
end;
/
create or replace function f_test(var_str in string) return t_ret_table PIPELINED
as
var_tmp varchar2(2000);
var_element varchar2(2000);
begin
var_tmp := var_str;
while instr(var_tmp,'|||')>0 loop
var_element := substr(var_tmp,1,instr(var_str,'|||')-1);
var_tmp := substr(var_tmp,instr(var_str,'|||')+3,length(var_tmp));
pipe row(var_element);
end loop;
pipe row(var_tmp);
return;
end f_test;
/
select * from table(f_test('a|||b|||c'));