Oracle拆分字串,字串分割的函數。

來源:互聯網
上載者:User

第一種:oracle字串分割和提取

分割

create or replace function Get_StrArrayLength(  av_str varchar2,  --要分割的字串  av_split varchar2  --分隔字元號)return numberis  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 varchar2is  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;

測試:   select Get_StrArrayStrOfIndex('songguojun$@111111537','$',0) from dual 

結果:

 

第二種:

本函數可以將“目標字串”以“指定字串”進行拆分,並通過表結構返回結果。代碼如下:

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     PIPELINEDAS    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;

建立完畢後,我們來測試一下,例如執行如下SQL:

select * from table(splitstr('Hello,Cnblogs!',','));

其輸出結果為一個兩行的表,如:

將行轉為列顯示:

select a.column_value v1,b.column_value v2 from (select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) a,(select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) bwhere a.rn=1 and b.rn=2

本函數可以將“目標字串”以“指定字串”進行拆分,並通過表結構返回結果。代碼如下:

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     PIPELINEDAS    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;

建立完畢後,我們來測試一下,例如執行如下SQL:

select * from table(splitstr('Hello,Cnblogs!',','));

其輸出結果為一個兩行的表,如:

將行轉為列顯示:

select a.column_value v1,b.column_value v2 from (select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) a,(select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) bwhere a.rn=1 and b.rn=2

建立完畢後,我們來測試一下,例如執行如下SQL:

select * from table(splitstr('Hello,Cnblogs!',','));

其輸出結果為一個兩行的表,如:

將行轉為列顯示:

select a.column_value v1,b.column_value v2 from (select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) a,(select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) bwhere a.rn=1 and b.rn=2

select * from table(splitstr('Hello,Cnblogs!',','));

其輸出結果為一個兩行的表,如:

將行轉為列顯示:

select a.column_value v1,b.column_value v2 from (select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) a,(select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) bwhere a.rn=1 and b.rn=2

其輸出結果為一個兩行的表,如:

將行轉為列顯示:

select a.column_value v1,b.column_value v2 from (select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) a,(select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) bwhere a.rn=1 and b.rn=2

將行轉為列顯示:

select a.column_value v1,b.column_value v2 from (select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) a,(select * from (select rownum rn,t.* from table(splitstr('Hello,Cnblogs!',',')) t)) bwhere a.rn=1 and b.rn=2

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.