I'm using this following code in Oracle pl/sql (Version: Oracle Database 11g Release 11.2.0.1.0)
select regexp_substr('A~B~C','[^~]+',1,level) output
from dual
connect by level <= length(regexp_replace('A~B~C','[^~]+')) + 1
which gives the following results
row1: A
row2: B
row3: C
That's perfect, however should I want to give a null value, ie:
select regexp_substr('~B~C','[^~]+',1,level) output
from dual
connect by level <= length(regexp_replace('~B~C','[^~]+')) + 1
I expected and wanted the following:
row1: <null>
row2: B
row3: C
but got this output:
row1: B
row2: C
row3: null
Am I doing the pl/sql code wrong? How can I make it work right?
I know that it is late now, but I think that what you (and I) needed was this:
you can combine
INSTR
undSUBSTR
to achive the desiered result:Because I used single characters as example in my question, but in practicality I'm using long strings in my project, for example 'How~do~I~do~this'
I came across this solution from OTN Oracle.com, thanks to chris227.
This will work even with single characters.
Hope this will help others looking for similair solutions.
You can use
NULLS FIRST
in theORDER BY
clauseTo quote from Oracle documentation
Since this problem was fresh on my mind and I happened to see this post, I respectfully submit my suggestion which builds on user3767503's answer. It reduces the number of function calls needed. It uses some 11g updates to
regexp_substr()
and makes use ofregexp_count()
which I believe was also introduced in 11g. It assumes the number of fields is the number of delimiters plus one.See this post for more info and detail on reading the regex pattern: Split comma seperated values to columns.
The bottom line is the commonly used regex pattern of
'[^<delimiter>]+'
to parse a string fails when there is a null in the list and should be avoided, IMHO.