This handles as many B_IDs as there are and NULL B_ID elements. Always test for unexpected values/conditions and make sure you are handling them! I suggest renaming that B_ID
column. The name implies a unique identifier which it obviously is not. Either that or some further normalization is required.
Note the regular expression which handles NULL list elements. The commonly used expression of '[^,]+'
for parsing lists does not handle NULL elements.
SQL> with tbl(B_ID, FirstName, LastName, email) as (
select 'B5,B6', 'Mo', 'Asif', 'xxx@yxz.com' from dual
union
select 'B1', 'Adam', 'chung', 'xxx@xyz.com' from dual
union
select 'B7,,B9', 'Lance', 'Link', 'llink@ape.org' from dual
union
select '', 'Mata', 'Hari', 'mhari@ape.org' from dual
)
SELECT REGEXP_SUBSTR(B_ID , '(.*?)(,|$)', 1, COLUMN_VALUE, NULL, 1 ) AS B_ID,
firstname, lastname, email
FROM tbl,
TABLE(
CAST(
MULTISET(
SELECT LEVEL
FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT(B_ID , ',' )+1
) AS SYS.ODCINUMBERLIST
)
);
B_ID FIRST LASTN EMAIL
------ ----- ----- -----------
B1 Adam chung xxx@xyz.com
B5 Mo Asif xxx@yxz.com
B6 Mo Asif xxx@yxz.com
B7 Lance Link llink@ape.org
Lance Link llink@ape.org
B9 Lance Link llink@ape.org
Mata Hari mhari@ape.org
7 rows selected.
SQL>