Oracle function implementation returns a dataset

Source: Internet
Author: User

/******************* Method 1 ***************** ************/
-- Create type
Create or replace type t_str is table of varchar2 (2000 );
-- Create function
Create or replace function uf_splitstr (I _str varchar2, I _split varchar2)
Return t_str
Pipelined is
Rochelle STR t_str: = t_str ();
L_tmpstr varchar2 (2000): = '';
Rochelle char varchar2 (1 );
Begin
If I _str = ''then
Return;
End if;
For I in 1 .. length (I _str)
Loop
Rochelle CHAR: = substr (I _str, I, 1 );
If l_char = I _split then
Pipe row (l_tmpstr );
Rochelle tmpstr: = '';
Else
L_tmpstr: = l_tmpstr | l_char;
End if;
End loop;
 
Pipe row (l_tmpstr );
 
Return;
End;
-- Example
Select * from table (uf_splitstr ('abcd, asdfad, adfadf ,',','))
/******************* Method 2 ***************** ************/
-- Directly use SQL
With t as (select ', aa 'str,', 'sep from dual)
Select substr (t. str,
T2.l,
Decode (instr (t. str, sep, l ),
0,
Length (t. str) + 1,
Instr (t. str, sep, l)-t2.l)
From t,
(Select level l from dual, t connect by rownum <= length (t. str) + 1) t2
Where substr (sep | t. str, l, 1) = sep


Author 12hao-Zhang Jian

Related Article

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.