1.自定义方法(长度相同,是指拆分后的每个长度相同):
create or replace package body by_pkg is
/*检查字符串分割后的实际个数*/
procedure SPLIT_SIZE(fundlist varchar2, ret out varchar2) as
ilen integer;
flist varchar2(2048);
begin
ret := '0';
flist := trim(fundlist);
if flist is null or length(flist) = 0 then
ret := '0';
else
select length(flist) - length(replace(flist,',')) + 1 into ilen from dual;
ret := '' || ilen;
end if;
end;
procedure SPLIT_STR( fundcodelist in varchar2,
flag out integer,
msg out varchar2,
thecursor IN OUT cursor_define.weavercursor)
as
i number;
ret varchar2(255);
funcode_size number ;
begin
i :=0;
by_stkpool_pkg.SPLIT_SIZE(fundcodelist,ret); --获取funcodelist 的个数
funcode_size := INSTR(fundcodelist, ',', 1, 1); --单个fundcode的长度
loop
i :=i+1;
if i > to_number(ret) then
i :=0;
exit;
end if;
if to_number(ret) =1 then
single_funcode :=fundcodelist;
else
single_funcode := SUBSTR(fundcodelist,funcode_size *(i-1) +1 , funcode_size - 1);
end if;
end loop;
//操作数据
Exception
when others then
rollback;
end;
end by_pkg;
2.使用REGEXP_SUBSTR 函数(拆分后长度可以不同):
select REGEXP_SUBSTR('123,12' ,'[^,]+', 1, LEVEL) STR from (select rownum rn from dual) CONNECT BY LEVEL <= REGEXP_COUNT('123,12', ',') + 1 and rn = prior rn and prior dbms_random.value is not null;
这里是逗号分割;
3.两列拆分合并(前提两列拆分的个数相同):
select a.STR1, b.STR2
from (select rownum as num1,
REGEXP_SUBSTR('119125,100994,399323,100926,000989,103566',
'[^,]+',
1,
LEVEL) STR1
from (select rownum rn from dual)
CONNECT BY LEVEL <= REGEXP_COUNT('119125,100994,399323,100926,000989,103566',
',') + 1
and rn = prior rn
and prior dbms_random.value is not null) a,
(select rownum as num2,
REGEXP_SUBSTR('0,1,0,0,1,1', '[^,]+', 1, LEVEL) STR2
from (select rownum rn from dual)
CONNECT BY LEVEL <= REGEXP_COUNT('0,1,0,0,1,1', ',') + 1
and rn = prior rn
and prior dbms_random.value is not null) b
where a.num1 = b.num2
结果:
写的不好请多多指教。