Oracle string segmentation Function

Source: Internet
Author: User

Create or replace type mytable as table of varchar2 (100)

Create or replace function split (SRC varchar2, delimiter varchar2)
Return mytable is
Psrc varchar2 (500 );
A mytable: = mytable ();
I number: = 1 ;--
J number: = 1;
Begin
Psrc: = rtrim (ltrim (SRC, delimiter), delimiter );
Loop
I: = instr (psrc, delimiter, J );
-- Dbms_output.put_line (I );
If I> 0 then
A. Extend;
A (A. Count): = trim (substr (psrc, J, I-j ));
J: = I + 1;
-- Dbms_output.put_line (a (A. Count-1 ));
End if;
Exit when I = 0;
End loop;
If j <length (psrc) then
A. Extend;
A (A. Count): = trim (substr (psrc, J, length (psrc) + 1-j ));
End if;
Return;
End;

Create or replace function split (SRC varchar2, delimiter varchar2) return mytable is
Psrc varchar2 (500); A mytable: = mytable (); I number: = 1 ;--
J number: = 1;
Begin
Psrc: = rtrim (ltrim (SRC, delimiter), delimiter); loop I: = instr (psrc, delimiter, J );
-- Dbms_output.put_line (I );
If I> 0 then A. Extend; A (A. Count): = trim (substr (psrc, J, I-j); j: = I + 1;
-- Dbms_output.put_line (a (A. Count-1 ));
End if; exit when I = 0;
End loop; If j <length (psrc) Then. extend; A (. count): = trim (substr (psrc, J, length (psrc) + 1-j ));
End if; return;
End;
/

An array is used as a query condition for select in.

Select * from student where ID in (select * from table (cast (split ('201312', ',')
Mytable); select * from student where ID in (select * from table (cast (split ('20170101', ',')
Mytable )));

select * from student where ID in (select ID from student where id = '001' Union select * from table (cast (split ('123 ',',') as
mytable)

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.