oracle中对字符串进行分割,并反回随机段
--测试数据 select fun_spilt_draw('1,2,3,4,5,6,7') a from dual
--待处理数据 strtext
--定义一个函数分割 返回字符串中的一个随机段
create or replace function fun_spilt_draw (strtext in varchar)
return varchar2 is
strreturn varchar2 (50);
icount integer;
begin
if(length(strtext)<=0) then
return '';
end if;
--获取总记录数
with ab as (select * from (select substr(t,1,instr(t,',',1)-1) count,rownum rn from (
select substr(s,instr(s,',',1,rownum)+1)||',' as t,rownum as d ,instr(s,',',1,rownum)+1 from (
select ','|| strtext ||'' as s from dual
)connect by instr(s,',','1',rownum)>1)))
select count(*) into icount from ab;
--随机取一行
SELECT ABS(dbms_random.value(1,icount)) into icount FROM dual;
--获取当前行字段值
select count into strreturn from( select substr(t,1,instr(t,',',1)-1) count,rownum rn from (
select substr(s,instr(s,',',1,rownum)+1)||',' as t,rownum as d ,instr(s,',',1,rownum)+1 from (
select ','|| strtext ||'' as s from dual
)connect by instr(s,',','1',rownum)>1)) a where rn = icount;
return strreturn;
end fun_spilt_draw;