Oracle 字符串分割

前端之家收集整理的这篇文章主要介绍了Oracle 字符串分割前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

分割函数一:

字符串分割:

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 @H_502_42@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:@H_502_42@=ltrim(rtrim(av_str));
  lv_str:@H_502_42@=concat(lv_str,av_split);
  lv_length:@H_502_42@=av_index;
  if lv_length@H_502_42@=0 then
      lv_strOfIndex:@H_502_42@=substr(lv_str,1,av_split)@H_502_42@-length(av_split));
  else
      lv_length:@H_502_42@=av_index@H_502_42@+1;
     lv_strOfIndex:@H_502_42@=substr(lv_str,av_split,av_index)@H_502_42@+length(av_split),lv_length)@H_502_42@-instr(lv_str,av_index)@H_502_42@-length(av_split));
  end if;
  return  lv_strOfIndex;
end Get_StrArrayStrOfIndex;

 

分割函数二:

--函数可以将“目标字符串”以“指定字符串”进行拆分,并通过表结构返回结果。代码如下:
CREATE @H_502_42@OR REPLACE TYPE str_split IS TABLE OF VARCHAR2 (4000);
CREATE @H_502_42@OR REPLACE FUNCTION splitstr(p_string @H_502_42@IN VARCHAR2,p_delimiter @H_502_42@IN VARCHAR2)
    RETURN str_split 
    PIPELINED
AS
    v_length   NUMBER :@H_502_42@= LENGTH(p_string);
    v_start    NUMBER :@H_502_42@= 1;
    v_index    NUMBER;
BEGIN
    WHILE(v_start @H_502_42@<= v_length)
    LOOP
        v_index :@H_502_42@= INSTR(p_string,p_delimiter,v_start);

        IF v_index @H_502_42@= 0
        THEN
            PIPE ROW(SUBSTR(p_string,v_start));
            v_start :@H_502_42@= v_length @H_502_42@+ 1;
        ELSE
            PIPE ROW(SUBSTR(p_string,v_start,v_index @H_502_42@- v_start));
            v_start :@H_502_42@= v_index @H_502_42@+ 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,,1) from dual;

分享图片

事例三:

select @H_502_42@* from table(splitstr(HJJHTSCYJD_TB13522018,));

分享图片

事例四:对各个表总数据求和

SELECT sum("count") FROM
(select count(1) "count" from HJJHTSCYJD_TB13522018 union @H_502_42@all 
 select count(1) "count" from HJJHTSCYJD_2012_TB13632018 union @H_502_42@all 
 select count(1) "count" from HJJHTSCYJD_2013_TB13642018 union @H_502_42@all 
 select count(1) "count" from HJJHTSCYJD_2014_TB13652018 union @H_502_42@all 
 select count(1) "count" from HJJHTSCYJD_2015_TB13662018 union @H_502_42@all 
 select count(1) "count" from HJJHTSCYJD_2016_TB13672018 union @H_502_42@all
 select count(1) "count" from HJJHTSCYJD_2017_TB16522018);

分享图片

猜你在找的Oracle相关文章