Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Required help to handle null in regexp_substr

AkshsJun 23 2016 — edited Jun 23 2016

Hi All,

I'm trying to  separate a string into column and the delimiter is "," .

create table base_tab(id number,seq number,lines varchar2 (100));

insert into base_tab values (1,1,'a,b,c,d,e,f');

insert into base_tab values (1,2,'x,f,c,d,e,f');

insert into base_tab values (2,1,'p,b,c,f,e,f');

with convert_dat as

(SELECT  id,REGEXP_SUBSTR (lines, '[^,]+', 1, 1)    AS A_1

,       REGEXP_SUBSTR (lines, '[^,]+', 1, 2)    AS B_1

,       REGEXP_SUBSTR (lines, '[^,]+', 1, 3)    AS C_1

,       REGEXP_SUBSTR (lines, '[^,]+', 1, 4)    AS D_1

,       REGEXP_SUBSTR (lines, '[^,]+', 1, 5)    AS E_1

,       REGEXP_SUBSTR (lines, '[^,]+', 1, 6)    AS F_1

FROM    base_tab where id =1 order by seq)

select * from convert_dat;

IDA_1B_1C_1D_1E_1F_1
1abcdef
1xfcdef

where I'm getting the proper output. But while value comes like  'l,m,,o,,f'  that time its causing problem. it will take the next value 'o' instead of null.

insert into base_tab values (1,1,'l,m,,o,,f');

again run the query:

with convert_dat as

(SELECT  id,REGEXP_SUBSTR (lines, '[^,]+', 1, 1)    AS A_1

,       REGEXP_SUBSTR (lines, '[^,]+', 1, 2)    AS B_1

,       REGEXP_SUBSTR (lines, '[^,]+', 1, 3)    AS C_1

,       REGEXP_SUBSTR (lines, '[^,]+', 1, 4)    AS D_1

,       REGEXP_SUBSTR (lines, '[^,]+', 1, 5)    AS E_1

,       REGEXP_SUBSTR (lines, '[^,]+', 1, 6)    AS F_1

FROM    base_tab

where id =1

order by seq)

select * from convert_dat;

Output:-

IDA_1B_1C_1D_1E_1F_1
1abcdef
1lmof0-
1xfcdef

require output:-

IDA_1B_1C_1D_1E_1F_1
1abcdef
1lmof
1xfcdef
Thanks in advance
This post has been answered by BluShadow on Jun 23 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 21 2016
Added on Jun 23 2016
9 comments
2,222 views