--測試數據 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;
dom