9

I have a table like below:

-------------
ID   | NAME
-------------
1001 | A,B,C
1002 | D,E,F
1003 | C,E,G
-------------

I want these values to be displayed as:

-------------
ID   | NAME
-------------
1001 | A
1001 | B
1001 | C
1002 | D
1002 | E
1002 | F
1003 | C
1003 | E
1003 | G
-------------

I tried doing:

select split('A,B,C,D,E,F', ',') from dual; -- WILL RETURN COLLECTION

select column_value
from table (select split('A,B,C,D,E,F', ',') from dual); -- RETURN COLUMN_VALUE
7
  • Will this work? SELECT COLUMN_VALUE FROM TABLE(split('A,B,C,D,E,F', ',')) By the way what Oracle version do you use? Commented May 14, 2014 at 8:52
  • As u can see, I already tried doing this. I want corresponding value of 'name' column also. Commented May 14, 2014 at 8:56
  • Ohh, I meant, I want corresponding value of 'ID' column also. Commented May 14, 2014 at 9:04
  • What is split? Is this a function you're supposed to write? Commented May 14, 2014 at 9:12
  • 1
    @vipin.huddar Really? Oracle 11gR2 tells me "no such function". Where in the Oracle docs did you find this? Commented May 14, 2014 at 9:34

7 Answers 7

7

Try using below query:

 WITH T AS (SELECT 'A,B,C,D,E,F' STR  FROM DUAL)   SELECT    
 REGEXP_SUBSTR (STR, '[^,]+', 1, LEVEL) SPLIT_VALUES  FROM T 
 CONNECT BY LEVEL <= (SELECT LENGTH (REPLACE (STR, ',', NULL)) FROM T)

Below Query with ID:

WITH TAB AS 
(SELECT '1001' ID, 'A,B,C,D,E,F' STR FROM DUAL
)
SELECT    ID, 
REGEXP_SUBSTR (STR, '[^,]+', 1, LEVEL) SPLIT_VALUES  FROM TAB 
CONNECT BY LEVEL <= (SELECT LENGTH (REPLACE (STR, ',', NULL)) FROM TAB);

EDIT: Try using below query for multiple IDs and multiple separation:

WITH TAB AS 
(SELECT '1001' ID, 'A,B,C,D,E,F' STR FROM DUAL
UNION
SELECT '1002' ID, 'D,E,F' STR FROM DUAL
UNION
SELECT '1003' ID, 'C,E,G' STR FROM DUAL
)
select id, substr(STR, instr(STR, ',', 1, lvl) + 1, instr(STR, ',', 1, lvl + 1) - instr(STR, ',', 1, lvl) - 1) name 
from
( select ',' || STR || ',' as STR, id from TAB ),
( select level as lvl from dual connect by level <= 100 )
where lvl <= length(STR) - length(replace(STR, ',')) - 1

order by ID, NAME
Sign up to request clarification or add additional context in comments.

6 Comments

I want value from 'ID' column as well.
@user2531405: What is the maximum number of commas expected in your name column?
Not fixed. There can be any number of comma separated values, for a particular ID.
@Nisha This won't work for multiple IDs in the input data (single-row subquery returns more than one row) - I think you need to add a WHERE clause for the ID in your subquery for the LENGTH.
Thanks for the help, I got some direction. But value of ID is hard coded, which will not be the case with me. Ya, and as Frank said, it will not work for multiple IDs.
|
7

There are multiple options. See Split comma delimited strings in a table in Oracle.

Using REGEXP_SUBSTR:

SQL> WITH sample_data AS(
  2  SELECT 10001 ID, 'A,B,C' str FROM dual UNION ALL
  3  SELECT 10002 ID, 'D,E,F' str FROM dual UNION ALL
  4  SELECT 10003 ID, 'C,E,G' str FROM dual
  5  )
  6  -- end of sample_data mimicking real table
  7  SELECT distinct id, trim(regexp_substr(str, '[^,]+', 1, LEVEL)) str
  8  FROM sample_data
  9  CONNECT BY LEVEL <= regexp_count(str, ',')+1
 10  ORDER BY ID
 11  /

        ID STR
---------- -----
     10001 A
     10001 B
     10001 C
     10002 D
     10002 E
     10002 F
     10003 C
     10003 E
     10003 G

9 rows selected.

SQL>

Using XMLTABLE:

SQL> WITH sample_data AS(
  2  SELECT 10001 ID, 'A,B,C' str FROM dual UNION ALL
  3  SELECT 10002 ID, 'D,E,F' str FROM dual UNION ALL
  4  SELECT 10003 ID, 'C,E,G' str FROM dual
  5  )
  6  -- end of sample_data mimicking real table
  7  SELECT id,
  8        trim(COLUMN_VALUE) str
  9  FROM sample_data,
 10       xmltable(('"'
 11          || REPLACE(str, ',', '","')
 12          || '"'))
 13  /

        ID STR
---------- ---
     10001 A
     10001 B
     10001 C
     10002 D
     10002 E
     10002 F
     10003 C
     10003 E
     10003 G

9 rows selected.

Comments

2

i solved similar problem this way...

    select YT.ID,
           REPLACE(REGEXP_SUBSTR(','||YT.STR||',',',.*?,',1,lvl.lvl),',','') AS STR
    from YOURTABLE YT
    join (select level as lvl 
          from dual 
          connect by level <= (select max(regexp_count(STR,',')+1) from YOURTABLE)
         ) lvl on lvl.lvl <= regexp_count(YT.STR,',')+1

Comments

1

You may try something like this:

CREATE OR REPLACE TYPE "STR_TABLE"
as table of varchar2


create or replace function GetCollection( iStr varchar2, iSplit char default ',' ) return STR_TABLE as
pStr varchar2(4000) := trim(iStr);
rpart varchar(255);
pColl STR_TABLE := STR_TABLE();
begin
   while nvl(length(pStr),0) > 0 loop
         pos := inStr(pStr, iSplit );
         if pos > 0 then
            rpart := substr(pStr,1, pos-1);
            pStr  := substr(pStr,pos+1,length(pStr));
         else
            rpart := pStr;
            pStr := null;
         end if;
         if rpart is not null then
           pColl.Extend;
           pColl(pColl.Count) := rpart;
         end if;
   end loop;
   return pColl;
end;

1 Comment

Can't I do it through a single query, as I want to avoid adding a new function or something like that on my required DB environment.
1

Do not use CONNECT BY or REGEXP which results in a Cartesian product on a complex query. Furthermore the above solutions expect you know the possible results (A,B,C,D,E,F) rather than a list of combinations

Use XMLTable:

SELECT c.fname, c.lname,
trim(COLUMN_VALUE) EMAIL_ADDRESS
 FROM 
  CONTACTS c, CONTACT_STATUS s,
  xmltable(('"'
  || REPLACE(EMAIL_ADDRESS, ';', '","')
  || '"'))
where  c.status = s.id

The COLUMN_VALUE is a pseudocolumn that belongs to xmltable. This is quick and correct and allows you to reference a column w/o know its values.

This takes the column and makes a table of values "item","item2","item3" and automatically joins to its source table (CONTACTS). This was tested on thousands of rows

Note the ';' in the xmltable is the separator in the column field.

Comments

1

I tried the solution of Lalit Kumar B and it worked so far. But with more data I ran into an performance issue (> 60 Rows, >7 Level). Therefore I used a more static variation, I would like to share as alternative.

WITH T AS (
      SELECT 1001 AS ID, 'A,B,C' AS NAME FROM DUAL
UNION SELECT 1002 AS ID, 'D,E,F' AS NAME FROM DUAL
UNION SELECT 1003 AS ID, 'C,E,G' AS NAME FROM DUAL
     )   --SELECT * FROM T
SELECT ID as ID,
       distinct_column AS NAME
  FROM ( SELECT t.ID,       
                trim(regexp_substr(t.NAME, '[^,]+', 1,1)) AS c1,
                trim(regexp_substr(t.NAME, '[^,]+', 1,2)) AS c2,
                trim(regexp_substr(t.NAME, '[^,]+', 1,3)) AS c3,
                trim(regexp_substr(t.NAME, '[^,]+', 1,4)) AS c4 -- etc.
           FROM T )
UNPIVOT ( distinct_column FOR cn IN ( c1, c2, c3, c4 ) )    


    ID NAME               
------ ------
  1001 A                    
  1001 B                    
  1001 C                    
  1002 D                    
  1002 E                    
  1002 F                    
  1003 C                    
  1003 E                    
  1003 G                    

9 Zeilen gewählt

1 Comment

Fantastic. I was stuck with the performance of the CONNECT BY approach, and this way works perfectly as long as you know the max. number of elements in the string -- which you can check by something like: select t.name, regexp_count(t.name, ',') from t group by t.name having regexp_count(t.name, ',') > 2;
0

this version works also with strings longer than one char:

select regexp_substr('A,B,C,Karl-Heinz,D','[^,]+', 1, level) from dual
  connect by regexp_substr('A,B,C,Karl-Heinz,D', '[^,]+', 1, level) is not null;

see How to split comma separated string and pass to IN clause of select statement

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.