Oracle/PLSQL Split Function

Oracle non fornisce in modo nativo un metodo per convertire una stringa, in cui ci sono valori numerici separati da un token, in un array.

Ecco una possibile soluzione:

create or replace type myTableType as table of number;

create or replace function str2tblp_str in varchar2 return 
myTableType
 
as
      
l_str   long default p_str || ',';
      
l_n        number;
      
l_data    myTableType :myTabletype();
  
begin
      loop
          
l_n :instrl_str',' );
          
exit when (nvl(l_n,0) = 0);
          
l_data.extend;
          
l_datal_data.count :ltrim(rtrim(substr(l_str,1,l_n-1)));
          
l_str :substrl_strl_n+);
      
end loop;
      
return l_data;
  
end;
/

da utilizzare nel seguente modo

select 
  
from all_users
 
where user_id in 
 (
       
select *
       
from THE 
       
(
          
select caststr2tbl'1, 3, 5, 7, 99' as mytableType 
            
from dual 
       

 )
 

USERNAME                          USER_ID CREATED
------------------------------ ---------- ---------
SYSTEM                                  
5 20-APR-99

fonti:

forums.oracle.com

asktom.oracle.com

powered by IMHO 1.3 

Print | posted on giovedì 5 gennaio 2006 18:57

Comments on this post

# re: Oracle/PLSQL Split Function

Requesting Gravatar...
I am happy to find a beautiful post for me. Now I am planing to leave for sleeping. I hope you will add that. Thanks
Left by solar collector on ago 17, 2010 3:53
Comments have been closed on this topic.