Oracle 字符串分割
阿新 • • 發佈:2018-08-25
sub lac 圖片 lec oop length col split 拆分
分割函數一:
字符串分割:
create or replace function Get_StrArrayLength
(
av_str varchar2, --要分割的字符串
av_split varchar2 --分隔符號
)
return number
is
lv_str varchar2(1000);
lv_length number;
begin
lv_str:=ltrim(rtrim(av_str));
lv_length:=0;
while instr(lv_str,av_split)<>0 loop
lv_length:=lv_length+1;
lv_str:=substr(lv_str,instr(lv_str,av_split)+length(av_split),length(lv_str));
end loop;
lv_length:=lv_length+1;
return lv_length;
end Get_StrArrayLength;
字符串提取:
create or replace function Get_StrArrayStrOfIndex ( av_str varchar2, --要分割的字符串 av_split varchar2, --分隔符號 av_index number --取第幾個元素 ) return varchar2 is lv_str varchar2(1024); lv_strOfIndex varchar2(1024); lv_length number; begin lv_str:=ltrim(rtrim(av_str)); lv_str:=concat(lv_str,av_split); lv_length:=av_index; if lv_length=0 then lv_strOfIndex:=substr(lv_str,1,instr(lv_str,av_split)-length(av_split)); else lv_length:=av_index+1; lv_strOfIndex:=substr(lv_str,instr(lv_str,av_split,1,av_index)+length(av_split),instr(lv_str,av_split,1,lv_length)-instr(lv_str,av_split,1,av_index)-length(av_split)); end if; return lv_strOfIndex; end Get_StrArrayStrOfIndex;
分割函數二:
--本函數可以將“目標字符串”以“指定字符串”進行拆分,並通過表結構返回結果。代碼如下: 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); IF v_index = 0 THEN PIPE ROW(SUBSTR(p_string, v_start)); v_start := v_length + 1; ELSE PIPE ROW(SUBSTR(p_string, v_start, v_index - v_start)); v_start := v_index + 1; END IF; END LOOP; RETURN; END splitstr;
事例一:
select Get_StrArrayLength(‘HJJHTSCYJD_TB13522018,HJJHTSCYJD_2012_TB13632018,HJJHTSCYJD_2013_TB13642018,HJJHTSCYJD_2014_TB13652018,HJJHTSCYJD_2015_TB13662018,HJJHTSCYJD_2016_TB13672018,HJJHTSCYJD_2017_TB16522018‘,‘,‘) from dual;
事例二:
select GET_STRARRAYSTROFINDEX(‘HJJHTSCYJD_TB13522018,HJJHTSCYJD_2012_TB13632018,HJJHTSCYJD_2013_TB13642018,HJJHTSCYJD_2014_TB13652018,HJJHTSCYJD_2015_TB13662018,HJJHTSCYJD_2016_TB13672018,HJJHTSCYJD_2017_TB16522018‘,‘,‘,1) from dual;
事例三:
select * from table(splitstr(‘HJJHTSCYJD_TB13522018,HJJHTSCYJD_2012_TB13632018,HJJHTSCYJD_2013_TB13642018,HJJHTSCYJD_2014_TB13652018,HJJHTSCYJD_2015_TB13662018,HJJHTSCYJD_2016_TB13672018,HJJHTSCYJD_2017_TB16522018‘,‘,‘));
事例四:對各個表總數據求和
SELECT sum("count") FROM (select count(1) "count" from HJJHTSCYJD_TB13522018 union all select count(1) "count" from HJJHTSCYJD_2012_TB13632018 union all select count(1) "count" from HJJHTSCYJD_2013_TB13642018 union all select count(1) "count" from HJJHTSCYJD_2014_TB13652018 union all select count(1) "count" from HJJHTSCYJD_2015_TB13662018 union all select count(1) "count" from HJJHTSCYJD_2016_TB13672018 union all select count(1) "count" from HJJHTSCYJD_2017_TB16522018);
Oracle 字符串分割