Splitting of comma delimited string CONNECT BY Clause
891071Feb 21 2013 — edited Feb 21 2013Hi ,
I have got a problem in splitting a comma separated strings into rows .
I am explaining the use case below :
i have a table x_imp
--> create table x_imp (id number, int_status varchar2(100),c_ref varchar2(30), s_ref varchar2(30));
I inserted values into the table :
insert into x_imp (id, int_status,c_ref,s_ref) values (1,'a1,a2,a3,a4','A','AS');
insert into x_imp (id, int_status,c_ref,s_ref) values (1,'b1,b2,b3,b4','B', 'BS');
insert into x_imp (id, int_status,c_ref,s_ref) values (1,'c1,c2,c3,c4', 'C', null);
insert into x_imp (id, int_status, cust_ref, site_ref) values (1,NULL, 'D', NULL);
I need to split the comma separated int_status into individual rows . That means my expected result is :
. What I need or looking for as expected result:
-----------
1, A, AS, a1
1, A, AS, a2
1, A, AS, a3
1, A, AS, a4
1, B, BS, b1
1, B, BS, b2
1, B, BS, b3
1, B, BS, b4
1, C, null, c1
1, C, null, c2
1, C, null, c3
1, C, null, c4
I currently have a solution using Regex . But this solution uses UNIQUE keyword .
The solution i have currently :
select UNIQUE c_ref,s_ref, regexp_substr(int_status,'[^,]+', 1, level) error_code
from x_imp
connect by regexp_substr(int_status, '[^,]+', 1, level) is not null;
I need a better solution . Any pointers ?
Thanks,
Bibin