Oracle SQL - Looking to get comma separated substrings into rows **without using CONNECT BY clause **
Example
input varchar2 string is = 'AAA,BBB,CCC,DDD'
Expecting output as below
AAA BBB CCC DDD
Oracle SQL - Looking to get comma separated substrings into rows **without using CONNECT BY clause **
Example
input varchar2 string is = 'AAA,BBB,CCC,DDD'
Expecting output as below
AAA BBB CCC DDD
If you compare
SQL> with
2 test (col) as
3 -- sample string
4 (select 'AAA,BBB,CCC,DDD' from dual)
5 select regexp_substr(col, '[^,]+', 1, level) val
6 from test
7 connect by level <= regexp_count(col, ',') + 1;
VAL
------------------------------------------------------------
AAA
BBB
CCC
DDD
SQL>
to code which doesn't use connect by
clause
SQL> with
2 test (col) as
3 -- sample string
4 (select 'AAA,BBB,CCC,DDD' from dual),
5 comma (col) as
6 -- add comma AFTER the COL string so that code that follows is simpler to maintain
7 (select col || ',' from test),
8 temp (val, str) as
9 -- recursion
10 (select substr (col, 1, instr (col, ',') - 1) as val,
11 substr (col, instr (col, ',') + 1) as str
12 from comma
13 where col like '%,%'
14 --
15 union all
16 --
17 select substr (str, 1, instr (str, ',') - 1),
18 substr (str, instr (str, ',') + 1)
19 from temp
20 where str like '%,%'
21 )
22 select val
23 from temp
24 where val is not null;
VAL
--------------------------------------------------------------------------------
AAA
BBB
CCC
DDD
SQL>
the question is: why would you want to do that?
You can use:
WITH bounds ( value, start_pos, end_pos ) AS (
SELECT value,
1,
INSTR( value, ',' )
FROM table_name
UNION ALL
SELECT value,
end_pos + 1,
INSTR( value, ',', end_pos + 1 )
FROM bounds
WHERE end_pos > 0
)
SELECT CASE end_pos
WHEN 0
THEN SUBSTR(value, start_pos )
ELSE SUBSTR(value, start_pos, end_pos - start_pos )
END AS value
FROM bounds;
At each recursion, you are only finding the position of the next comma in the string and you start looking from the position after the previous comma which:
So, according to the analysis performed in this question, it may be faster than using hierarchical queries.
Which for the sample data:
CREATE TABLE table_name (value) AS
SELECT 'AAA,BBB,CCC,DDD' FROM DUAL;
Outputs:
VALUE |
---|
AAA |
BBB |
CCC |
DDD |