A typical csv parsing approach is as follows:
WITH t(csv_str) AS ( SELECT '2035197553,2,S,14-JUN-14,,P' FROM dual UNION ALL SELECT '2035197553,2,S,14-JUN-14,,' FROM dual ) SELECT LTRIM(REGEXP_SUBSTR (',' || csv_str, ',[^,]*', 1, 1), ',') AS phn_nbr, LTRIM(REGEXP_SUBSTR (',' || csv_str, ',[^,]*', 1, 2), ',') AS phn_pos, LTRIM(REGEXP_SUBSTR (',' || csv_str, ',[^,]*', 1, 3), ',') AS phn_typ, LTRIM(REGEXP_SUBSTR (',' || csv_str, ',[^,]*', 1, 4), ',') AS phn_strt_dt, LTRIM(REGEXP_SUBSTR (',' || csv_str, ',[^,]*', 1, 5), ',') AS phn_end_dt, LTRIM(REGEXP_SUBSTR (',' || csv_str, ',[^,]*', 1, 6), ',') AS pub_indctr FROM t
I like to place a comma preceding my csv, and then I would count non-comma commas.
Search pattern explanation
The search pattern looks for the nth substring (the nth corresponds to the nth element in csv), which has the following:
-The program starts with the character ' , '
-Next, followed by the pattern ' [^,] '. This is just an inconsistent list expression. The caret, ^ , indicates that the characters in the list must not match.
. This inconsistent list of characters has a * quantifier, which means that this can happen 0 or more times.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Once a match is found, I would also use the LTRIM function to remove the comma after using the reg expression.
What you like about this approach is that the appearance of the search pattern will always match the occurrences of the comma.