I have a table consisting a list of names separated by comma. My goal is to separated them by rooms.
Room | Name
room1 | Anne,Amy
room2 | Ben,Bryan
My goal:
Room | Name
room1 | Anne
room1 | Amy
room2 | Ben
room2 | Bryan
I have read some solutions on how to split strings to rows, but are there alternatives to run on Oracle 8i. I have followed some articles to split them to rows like this:
create or replace function str2tbl( p_str IN varchar2 , p_delimiter in varchar2) return mytabletype
as
l_str long default p_str || p_delimiter;
l_n number;
l_data mytabletype := mytabletype();
begin
loop
l_n := instr( l_str, p_delimiter );
exit when (nvl(l_n,0) = 0);
l_data.extend;
l_data( l_data.count ) := ltrim(rtrim(substr(l_str,1,l_n-1)));
l_str := substr( l_str, l_n+1 );
end loop;
return l_data;
end str2tbl;
Then I do a SELECT from my table like below:
select * from the ( select cast(str2tbl( Name, ',' ) as mytableType )
from SPLITSTRING);
and got below result, but cant bring out values for Room column:
Name
Anne
Amy
Ben
Bryan
Is there any way to split to rows in Oracle 8i?
I am not sure if this is supported in 8i, I have tested this out in 12c, and it is working fine:
Table 'test' created
INSERT INTO test successful 1 row affected
INSERT INTO test successful 1 row affected
INSERT INTO test successful 1 row affected
Using XMLTABLE:
ROOM | NAMES
room1 | anne
room2 | amy
room2 | sheldon
room3 | penny
room3 | leonard