oracle把一个字段按逗号拆开(oracle将字段按指定符号拆开)
Oracle高手请接招:以逗号为分隔符把一列拆成3列
oracle 中可以用connect by把字符串按都好分割:
比如定义字符串:
pv_no_list :='23,34,45,56';
SELECT DISTINCT REGEXP_SUBSTR (pv_no_list,'[^,]+',1,LEVEL) as "token"
FROM DUAL
CONNECT BY REGEXP_SUBSTR (pv_no_list,'[^,]+',1,LEVEL) IS NOT NULL
order by 1
返回结果:
TOKEN
23
34
45
56
oracle如何拆分以逗号分隔的字符串为多列
oracle 中可以用connect by把字符串按都好分割:
比如定义字符串:
pv_no_list :='23,34,45,56';
SELECT DISTINCT REGEXP_SUBSTR (pv_no_list,'[^,]+',1,LEVEL) as "token"
FROM DUAL
CONNECT BY REGEXP_SUBSTR (pv_no_list,'[^,]+',1,LEVEL) IS NOT NULL
order by 1
返回结果:
TOKEN
23
34
45
56
Oracle中怎样用存储过程将一段字符串以逗号为界截取,并将得到的子串存到另一张表中
--通过游标遍历调用方法拆取存表:
--方法
create or replace function f_test_substr(
prm1 varchar2,??? --地
prm2 varchar2 ,?? --名称
prm3 varchar2,??? --IP
prm4 varchar2??? --结果
)
return varchar2
as
v_n int:=0;? --第几次取
v_l int;? --:截取次数
v_s varchar2(2000);? --处理拆分字符串
v_s1 varchar2(200);? --本次拆分
v_s2 varchar2(200);? --本次剩余
v_type varchar2(100);? --设备类型
v_code varchar2(100);? --设备编号
v_desc varchar2(100);? --设备描述
v_errmsg varchar2(200);
begin
v_l:=length(prm4)-length(replace(prm4,';',''));
v_s:=prm4||';';
v_s1:=substr(v_s,1,instr(v_s,';',1,1));
v_s2:=replace(v_s,v_s1,'');
while v_n=v_l loop
v_s2:=replace(v_s2,v_s1,'');
v_s1:=substr(v_s1,1,length(v_s1)-1);
v_type:=substr(v_s1,1,instr(v_s1,',',1,1)-1);
v_code:=substr(v_s1,instr(v_s1,',',1,1)+1,instr(v_s1,',',1,2)-instr(v_s1,',',1,1)-1);
v_desc:=substr(v_s1,instr(v_s1,',',1,2)+1,instr(v_s1||',',',',1,3)-instr(v_s1,',',1,2)-1);
insert into t_test_str(a,b,c,d,e,f) values(prm1,prm2,prm3,substr(v_type,instr(v_type,']',1)+1),v_code,v_desc);
v_n:=v_n+1;
v_s1:=substr(v_s2,1,instr(v_s2,';',1,1));
end loop;
commit;
return '成功';
exception
when others then
v_errmsg:=substr(sqlerrm,1,200);
return v_errmsg ;
end;
--存过
create or replace procedure p_test_substr1(o_result out varchar2)
as
v_a varchar2(20);
v_b varchar2(20);
v_c varchar2(20);
v_d varchar2(2000);
v_num int;
cursor c_str is
SELECT * from t_test_substr
;
begin
select count(1) into v_num from t_test_substr;
while v_num0 loop
open c_str;
loop
FETCH c_str INTO
v_a,v_b,v_c,v_d;
EXIT WHEN c_str%NOTFOUND;
o_result:=f_test_substr(v_a,v_b,v_c,v_d);
v_num:=v_num-1;
end loop;
CLOSE c_str;
end loop;
end;
效果见图:
oracle 如何将一个字段由逗号分割成多行
select b,
t.ca,
c.lv,instr(t.ca, ',', 1, c.lv) + 1,
substr(t.ca,
instr(t.ca, ',', 1, c.lv) + 1,
instr(t.ca, ',', 1, c.lv + 1) -
(instr(t.ca, ',', 1, c.lv) + 1)) AS c
from (select b,
',' || c || ',' AS ca,
c,
LENGTH(C),
length(c || ','),
REPLACE(c, ','),
length(REPLACE(c, ',')), --删除逗号的长度
nvl(length(REPLACE(c, ',')), 0), --空值长度为空,因此要将空值替换为0
length(c || ',') - nvl(length(REPLACE(c, ',')), 0) AS cnt --以逗号分隔的字符串个数,先在原来的长度上加1 计算去除所有逗号后字符串长度,再,拿这个
FROM TEST_A) t,
(select LEVEL lv from dual CONNECT BY LEVEL = 100) c
where c.lv = t.cnt --逗号数量在100以内