I have a database column which contains comma separated values. Is there any way to get the individual values from this column into different rows without using regular expression in Oracle.
Asked
Active
Viewed 677 times
-2
-
Whats wrong with REGEXP_SUBSTR? – JavaSheriff Feb 15 '17 at 14:10
-
1Of course there is, just use SUBSTR and INSTR (and possibly LENGTH). Did you do a search, even here on SO - you should find many examples. – mathguy Feb 15 '17 at 14:12
-
If it's WITHOUT using regular expression then perhaps the 'regex' tag is not required. – BriteSponge Feb 15 '17 at 14:20
-
See the documentation pages for [Splitting Delimited Strings](http://stackoverflow.com/documentation/oracle/1968/splitting-delimited-strings/6447/splitting-strings-using-a-pl-sql-function#t=201702151423202078632) – MT0 Feb 15 '17 at 14:24
2 Answers
3
Create a small function that converts the delimited list to a collection and then use a TABLE() collection expression to include it as separate rows:
PL/SQL Function:
CREATE OR REPLACE FUNCTION split_String(
i_str IN VARCHAR2,
i_delim IN VARCHAR2 DEFAULT ','
) RETURN SYS.ODCIVARCHAR2LIST DETERMINISTIC
AS
p_result SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST();
p_start NUMBER(5) := 1;
p_end NUMBER(5);
c_len CONSTANT NUMBER(5) := LENGTH( i_str );
c_ld CONSTANT NUMBER(5) := LENGTH( i_delim );
BEGIN
IF c_len > 0 THEN
p_end := INSTR( i_str, i_delim, p_start );
WHILE p_end > 0 LOOP
p_result.EXTEND;
p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, p_end - p_start );
p_start := p_end + c_ld;
p_end := INSTR( i_str, i_delim, p_start );
END LOOP;
IF p_start <= c_len + 1 THEN
p_result.EXTEND;
p_result( p_result.COUNT ) := SUBSTR( i_str, p_start, c_len - p_start + 1 );
END IF;
END IF;
RETURN p_result;
END;
/
Sample Data:
CREATE TABLE table_name ( id, list ) AS
SELECT 1, 'a,b,c,d' FROM DUAL UNION ALL -- Multiple items in the list
SELECT 2, 'e' FROM DUAL UNION ALL -- Single item in the list
SELECT 3, NULL FROM DUAL UNION ALL -- NULL list
SELECT 4, 'f,,g' FROM DUAL; -- NULL item in the list
Query:
SELECT t.id,
v.column_value AS value,
ROW_NUMBER() OVER ( PARTITION BY id ORDER BY ROWNUM ) AS lvl
FROM table_name t
LEFT OUTER JOIN
TABLE( split_String( t.list ) ) v
ON ( 1 = 1 )
Output:
ID VALUE LVL
---------- ------- ----------
1 a 1
1 b 2
1 c 3
1 d 4
2 e 1
3 (NULL) 1
4 f 1
4 (NULL) 2
4 g 3
MT0
- 113,669
- 10
- 50
- 103
0
Here's an SO post that uses XML instead of regexp_substr.
Hope that helps
EDIT: For those who don't want to follow the link, a simple example would be:
with testdata as (
select 'a,test,string,for,splitting,values' as col from dual
)
select extractvalue(x.column_value, 'e') as tokens
from testdata, xmltable ('e' passing xmlparse( content '<e>' || replace(col, ',', '</e><e>') || '</e>')) x;
Output:
TOKENS
a
test
string
for
splitting
values
There are limitations just like with regexp solution. But this approach does not use regexp as requested.
-
-
-
Maybe APC. Perhaps its better to just copy/paste ;) Thanks for the heads up – tbone Feb 15 '17 at 17:47