重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
/*
网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、重庆小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了浪卡子免费建站欢迎大家使用!
*根据某个字符拆分字符串
*/
CREATE OR REPLACE TYPE str_split IS TABLE OF VARCHAR2 (4000);
CREATE OR REPLACE FUNCTION splitstr(p_string IN VARCHAR2, p_delimiter IN VARCHAR2)
RETURN str_split
PIPELINED
AS
v_length NUMBER := LENGTH(p_string);
v_start NUMBER := 1;
v_index NUMBER;
BEGIN
WHILE(v_start = v_length)
LOOP
v_index := INSTR(p_string, p_delimiter, v_start);
/ 示范 /
SELECT * from table(splitstr('Hello,Cnblogs!',','));
创建与A相同结构的表A_temp
create or replace procedure busi_bm is
lstr varchar2(1000);
ln number;
mid a_test.id%type;
m_bm a_test.bm%type;
begin
for rec_dia in (select *
from a_test t) loop
if rec_dia.bm = replace(rec_dia.bm, ',') then
--只有一个值
insert into a_test_temp values(rec_dia.id,rec_dia.bm);
commit;
else
lstr := rec_dia.bm || ',';
loop
ln := instr(lstr, ',');--第一个逗号的索引 a,b,c
exit when(nvl(ln, 0) = 0);--没有逗号了,退出
m_bm := ltrim(rtrim(substr(lstr, 1, ln - 1)));--获得a
lstr := substr(lstr, ln + 1);--b,c
insert into a_test_temp values(rec_dia.id,m_bm);
commit;
end loop;
end if;
end loop;
end busi_bm;
执行上面的存储过程,写入到A_TEMP表。记得到了你要的结果。
记得采纳!
拆分sql后:
select
substr('Hello World',0,instr('Hello World','o')) as 首,
substr('Hello World',instr('Hello World','o')+1) as 尾
from dual;
有什么规定吗?
“ABCDEFGH”或是“A,B,C,D,E,F,G,H"
按符号拆分?按位置拆分?
substr()函数:截取a-b之间的或a 。如:substr('Hello World',0,5)截取1-5或
substr('Hello World',0,instr('Hello World','o'))截取1-"o"的位置
1.如果是","等符号:
select substr('Hello World',0,instr('Hello World','o')) from dual;
select substr('Hello World',instr('Hello World','o')+1) from dual;
或
select substr('Hello World',instr('Hello World','o')+1,length('Hello World')) from dual;
create or replace type strArray as table of varchar2(4020)
--此方法只能使用英文半角逗号(,)
create or replace function f_split(instr IN VARCHAR2) return strArray
as
l_tablen BINARY_INTEGER;
l_tab DBMS_UTILITY.uncl_array;
l_ret strArray:=strArray();
BEGIN
DBMS_UTILITY.comma_to_table(instr, l_tablen, l_tab);
FOR i IN 1 .. l_tablen LOOP
l_ret.EXTEND(1);
l_ret(i):=l_tab(i);
END LOOP;
return l_ret;
END;
select t.column_value as value from table(f_split('a,b,付')) t;
create or replace type strArray as table of varchar2(4020)
CREATE OR REPLACE FUNCTION f_split(p_str IN VARCHAR2, p_delimiter IN VARCHAR2)
RETURN strArray IS
TYPE ret_cur IS REF CURSOR;
ret_cur1 ret_cur;
g_str strarray:=strarray();
l_sql varchar2(500):='';
BEGIN
l_sql :='SELECT REGEXP_SUBSTR ('''||p_str||''',''[^'||p_delimiter||']+'',1,LEVEL) as valuesa
FROM DUAL
CONNECT BY REGEXP_SUBSTR ('''||p_str||''',''[^'||p_delimiter||']+'',1,LEVEL) IS NOT NULL';
OPEN ret_cur1 FOR l_sql ;
FETCH ret_cur1 BULK COLLECT INTO g_str;
CLOSE ret_cur1;
RETURN g_str;
END f_split;
select * from table(f_split('abc$d$e$','$') );