Sometimes the number of rows found using IS NOT NULL
on the output of REGEXP_SUBSTR
is the same as the number of rows from REGEXP_COUNT
and then they do the same thing and it is a matter of personal preference:
SELECT REGEXP_SUBSTR( '1,2,3', '\d+', 1, LEVEL ) AS value
FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT( '1,2,3', '\d+' )
and
SELECT REGEXP_SUBSTR( '1,2,3', '\d+', 1, LEVEL ) AS value
FROM DUAL
CONNECT BY REGEXP_SUBSTR( '1,2,3', '\d+', 1, LEVEL ) IS NOT NULL
both output:
VALUE
-----
1
2
3
However, sometimes the sub-string matched can be zero-width (which is equivalent to NULL
) and then they do not:
SELECT REGEXP_SUBSTR( '1,2,,4', '(\d*)(,|$)', 1, LEVEL, 1 ) AS value
FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT( '1,2,3', '(\d*)(,|$)' )
Outputs:
VALUE
------
1
2
(null)
4
(null)
and:
SELECT REGEXP_SUBSTR( '1,2,,4', '(\d*)(,|$)', 1, LEVEL, NULL, 1 ) AS value
FROM DUAL
CONNECT BY REGEXP_SUBSTR( '1,2,,4', '(\d*)(,|$)', 1, LEVEL, NULL, 1 ) IS NOT NULL
Outputs:
VALUE
------
1
2
and stops at the first NULL
value.
To get all the results you either need to use REGEXP_COUNT
or not have the REGEXP_SUBSTR()
function return NULL
values in the CONNECT BY
clause (so, in this example we could fix it by extracting the full match rather than just the capturing group; i.e. REGEXP_SUBSTR( '1,2,,4', '(\d*)(,|$)', 1, LEVEL ) IS NOT NULL
).