Oracle string splitting Function

Source: Internet
Author: User

 

Http://lwl0606.cmszs.com/archives/Oracle-Split-string-Function. Html

Create or replace type varchar2varray is varray (100) of varchar2 (40 );
/

Create or replace function sf_split_string (string varchar2, substring varchar2) return varchar2varray is
Len INTEGER: = length (substring );
Lastpos INTEGER: = 1-len;
Pos integer;
Num integer;
I INTEGER: = 1;
RET varchar2varray: = varchar2varray (null );
Begin
Loop
POs: = instr (string, substring, lastpos + Len );
If POS> 0 then -- found
Num: = pos-(lastpos + Len );
Else -- not found
Num: = length (string) + 1-(lastpos + Len );
End if;

If I> ret. Last then
Ret. Extend;
End if;

RET (I): = substr (string, lastpos + Len, num );

Exit when Pos = 0;
Lastpos: = Pos;
I: = I + 1;
End loop;

Return ret;
End;
/

SQL> select * from table (cast (
2 sf_split_string ('abc, defgh, ijklmn, opqrst ',', ') as varchar2varray ));

Column_value
----------------------------------------
ABC
Defgh
Ijklmn
Opqrst

SQL> select * from table (cast (
2 sf_split_string ('abc // defgh/ijklmn // opqrst', '//') as varchar2varray ));

Column_value
----------------------------------------
ABC
Defgh/ijklmn
Opqrst

Http://lwl0606.cmszs.com/archives/Oracle-Split-string-Function. Html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.