Comma separated column values into row values

Hi all,
i am selecting the data from TABLE A ( id number ,rights varchar2 ). result set as
ID     RIGHTS
1     M,P,Y,N,C,P
4     N,E,A
10     N,C,R,P
but i want the output as
ID     RIGHTS
1     M
1 P
1 Y
1 N
1 C
1 P
4     N
4 E
4 A
10     N
10 C
10 R
10 P
kindly share your idea's to get the desired results.
thanks in advance
Edited by: 887268 on Nov 30, 2012 11:12 PM

Try this
SQL> WITH a(id, rights) AS
  2  (
  3  SELECT 1, 'M,P,Y,N,C,P' FROM dual UNION ALL
  4  SELECT 4, 'N,E,A' FROM dual UNION ALL
  5  SELECT 10, 'N,C,R,P' FROM dual)
  6  SELECT id, regexp_substr(rights, '[^,]+',1,level) res
  7  FROM a
  8  CONNECT BY  level - 1 <= regexp_count(rights,',')  /*  regexp_count:- 11g */
  9  AND prior id = id
10  AND prior sys_guid() IS NOT NULL /* sys_guid:- 11g */
11  ORDER BY id
12  /
        ID RES
         1 M
         1 P
         1 Y
         1 N
         1 C
         1 P
         4 N
         4 A
         4 E
        10 N
        10 R
        ID RES
        10 C
        10 P
13 rows selected.Thanks!
Edited by: Ashu_Neo on Dec 1, 2012 9:43 PM
-- Added comment

Similar Messages

  • SQL Select with comma separated column value

    Hi All
    ASP VBScript
    I have a DB column named allowed_contracts that stores a
    comma separated
    list of of values e.g. 3, 5, 19, 44, 52
    I need to select records based on a variable called
    varContractList that
    contains another comma separated list i.e. 5, 44, 52
    I only want to select records where the allowed_contracts
    column contains
    each of the varContractList,
    For example only return records that have 5 or 44 or 52 in
    thier
    allowed_contracts column.
    My brain is now in a persistant vegetive state trying to work
    this out so
    any ideas would be much appreciated.
    Regards
    Bren

    Hi Jules
    Cheers for this.
    I was trying to be a bit cute (lazy even) by storing the
    project ID's as a
    comma delimted string but as we see it doesn't always pay to
    be lazy. Serves
    me right for destroying brain cells by drinking the Welshpool
    beer the other
    week whilst visiting mates down that neck of the woods. :-))
    Time for another table me thinks.
    Rgds
    Bren
    "Julian Roberts" <[email protected]> wrote in message
    news:e7i13f$mup$[email protected]..
    > Fatal flaw here Bren. In a relational database, one
    shouldn't really store
    > foreign keys as a comma delimted string. One should have
    a 3 table
    > structure. eg
    >
    > Products:
    > ProductID
    > Product
    >
    > Categories:
    > CategoryID
    > Category
    >
    > ProductCategories:
    > ProductID
    > CategoryID
    >
    > So, from the table ProductCategories, a product can
    belong to many
    > categories. When doing a front end search to find
    products in multiple
    > categories, products can be shown thus:
    >
    > select * from Products where ProductID in (select
    ProductID from
    > ProductCategories where CategoryID in (5,44))
    >
    > --
    > Jules
    >
    http://www.charon.co.uk/charoncart
    > Charon Cart 3
    > Shopping Cart Extension for Dreamweaver MX/MX 2004
    >
    >
    >
    >
    >

  • Comma separated column into rows for each value

    experts,
    I have a two column table. The second column has comma separated string values. How can I put the values separate for each comma separated and make one row for each value?
    example,
    column1 |   StringColumn
    s1          |   test, joy, happy
    s2          |  USA, England, India
    I want the result to be like below
    column1   |     StringColumn
    s1            |      test
    s1            |      joy
    s1            |      happy
    s2            |     USA
    s2            |     England
    s2            |     India
    thanks in advance
    ebro

    Hello ebro,
    See the following for a possible solution:
    http://gallery.technet.microsoft.com/scriptcenter/Convert-Small-CSV-Value-to-ffc142ff
    Olaf Helper
    [ Blog] [ Xing] [ MVP]

  • [svn:osmf:] 15983: Updating VideoQoSPluginMetadataSynthesizer to create comma separated string values for all of the available keys .

    Revision: 15983
    Revision: 15983
    Author:   [email protected]
    Date:     2010-05-10 04:47:46 -0700 (Mon, 10 May 2010)
    Log Message:
    Updating VideoQoSPluginMetadataSynthesizer to create comma separated string values for all of the available keys.
    Modified Paths:
        osmf/trunk/apps/samples/plugins/VideoQoSPlugin/src/org/osmf/qos/VideoQoSPluginMetadataSyn thesizer.as

    Rob:
    "but the sad thing is, that managers will most likely respond with a "This used to be fast in MSSQL, but now it isn't any more in Oracle. Oracle is so slow ...""
    On the bright side, it sounds like most of the database calls are implemented as stored procedures, so there is an opportunity to do it right (in Oracle terms) in the stored procedures.
    I did a similar conversion a while back, converting bad SQLServer procedures to good Oracle procedures. Everyone said "Oracle is much faster that SQLServer"
    John

  • SharePoint 2013 - Split comma separated Column in to different row items in SharePoint 2013 Workflow or Event Handler

    I have a requirement to take a String column that allows multiple fields i.e. "101;102,103,104",
    and using a SPD 2013 workflow I want to split there values and then insert each Values into different list as separate items.
    Eg:
    I have a LIST A with the columns CustID, CustName and LIST B with the same Column Name with the below values
    LIST A:
    Cust ID
    CustName
    101
    ABC, XYZ, CTSS, WIPROS, TCSS
    I want to insert the above comma separated values in the below format(different Row)
    LIST B:
    CustID
    CustName
    101
    ABC
    101
    XYZ
    101
    CTSS
    101
    WIPROS
    101
    TCSS

    Hi,
    Here is a codeplex solution can meet your reference:
    Plumsail String Workflow Actions for SharePoint 2013 and SharePoint Online
    http://spworkflowactions.codeplex.com/
    We can use Split string workflow action to split the CustName value, then insert it to ListB.
    More information:
    http://www.codeproject.com/Articles/749471/String-processing-workflow-actions-for-SharePoint
    Best Regards
    Dennis Guo
    TechNet Community Support

  • Comma Separated columns - Search

    Hi fellow,
    I need to query my db with a comma separated list but the results are not correct...What is the reason?
    My typical db problem_code column has valuas as :
    Problem_code
    1
    2,3
    5,6,7,8
    5
    And the list values I am sending as ("1","3")...etc
    SELECT * from table
    whereproblem_code in ('2','1')problem_code in ('2','1') 
    Thank you in advance..

    If you're sking SQL questions, you'd better off asking on a SQL forum.  But here you are, so let's have a look at it.
    Firstly, the other thing to always mention when raising an SQL question is to articulate what DB system you're using.  Because all of them handle SQL differently, and have their own extensions.
    In fact reading this is probably a good idea: http://www.catb.org/~esr/faqs/smart-questions.html
    Problem_code
    1
    2,3
    5,6,7,8
    5
    And the list values I am sending as ("1","3")...etc
    SELECT * from table
    whereproblem_code in ('2','1')problem_code in ('2','1') 
    Thank you in advance..
    What are you seeing here that's not expected (I presume the double-up of problem_code in ('2','1') is a typo)?
    Are you wanting to get any rows from TABLE which have either 1 or 2 in them?  That's not what you're asking for in your WHERE statement though, is it?
    Really, you should be normalising your data, because whilst you can write contorted logic to do what you need here, it will not perform well and certainly will not scale.  So it for be somewhat poor practice to perpetuate the current situation if it's at all possible not to.   The best thing to do is to normalise your data and refactor your code.  Which will probably mean going to your boss and say "this DB schema is f*cked, and needs reworking.  Needs reworking".
    Depending on your DB system, you're probably going to need to loop over the list you want to filter on and do a "WHERE problem_code LIKE" on each element of the list.  I think Oracle can do the sort of query you want to do - match elements of one list from another list - but I'm not sure.
    Adam

  • Columns converted into rows.

    Hello-
    I have a table with 5 columns contains distinct rows.
    Example:
    COL_A     COL_B     COL_C     COL_D     COL_E
    1051     25000     7/17/2009     7/20/2009     190409949A
    1051     V4581     12/9/2009     12/11/2009     407829924A
    1051     78659     7/21/2010     7/25/2010     403021811A
    1051     72400     8/21/2009     8/31/2009     407381779M
    1051     73300     2/6/2010     2/13/2010     403506467A
    1051     5849     3/23/2010     3/30/2010     402442165A
    1051     6827     9/16/2009     9/23/2009     400245360W
    1051     4254     3/11/2009     3/19/2009     404824298A
    1051     2768     10/16/2009     10/17/2009     401987971A
    1051     53081     12/23/2009     12/27/2009     406889222Aso on
    Now I want to convert this 10 rows into one column like :
    1051 2009717250200972090409949 10512009129452009121190409949 1051201072178201072503021811 1051 200982120097002009 83107381779 ......so on
    ie first 10 records will become one column and so on......
    So for every 10 records I have to make it one column as above results. and there are two spaces after 1051(means first column and after last column col_E there are 6 spaces then second row will start.
    Can you please help me out how can we do it.
    Thanks in advance!
    Thanks
    Nick
    Edited by: user12020842 on Apr 21, 2011 12:23 PM

    Try this,
    SELECT REPLACE ( MAX (SYS_CONNECT_BY_PATH ( str, '@#')), '@#')
      FROM (SELECT col_a || ' ' || col_b || col_c || col_D || col_e || '      ' str,
                   CEIL (ROWNUM / 10) lvl,
                   ROW_NUMBER () OVER (PARTITION BY CEIL (ROWNUM / 10) ORDER BY col_a) rn
              FROM Table_name)
    START WITH rn = 1
    CONNECT BY PRIOR rn + 1 = rn
           AND PRIOR lvl = lvl
    GROUP BY lvlG.

  • How to convert colon separated column values in multiple rows in report

    Hi All,
    I want to display colon separated values from a column, in a multi row in report.
    For example i have a column1 in a table with value 'A:B:C' , column2 has value '1'.
    i want to show in a report three rows using these two columns like
    column1 column2
    A 1
    B 1
    C 1

    Here's one way:
    SQL> create table test (col1 varchar2(20), col2 number);
    Table created.
    SQL> insert all
      2    into test values ('A:B:C', 1)
      3    into test values ('Dg:Ezs', 2)
      4  select * from dual;
    2 rows created.
    SQL> select
      2    t.col2,
      3    regexp_substr(t.col1, '\w+', 1, t2.column_value) c1
      4  from test t,
      5    table(cast(multiset(select level
      6                        from dual
      7                        connect by level <= length(t.col1) - length(replace(t.col1, ':', '')) + 1
      8                       ) as sys.odcinumberlist )) t2
      9  order by 2, 1;
          COL2 C1
             1 A
             1 B
             1 C
             2 Dg
             2 Ezs
    SQL>Edited by: Littlefoot on Jan 31, 2012 10:13 AM

  • Performance problem: Converting rows to a comma separated column using STUFF() and self join

    Hi,
    This might be a really dumb one to ask but I am currently working on a table that has sequential data for steps that an invoice goes through in a particular system. Here is how it looks:
    ID InvoiceID
    InvoiceSteps
    Timestamp
    283403 0000210121_0002_2013
    Post FI Invoice
    2013-07-01 19:07:00.0000000
    389871 0000210121_0002_2013
    Clear Invoice
    2013-08-25 14:02:00.0000000
    Here is my extremely slow query that converts multiple rows of an invoice into a single one with 'InvoiceSteps' listed according to their timestamps in a sequential manner separated by commas.
    SELECT [InvoiceID],
    [InvoiceSteps] = STUFF((
    SELECT ',' + ma.InvoiceSteps
    FROM invoices ma
    WHERE m.InvoiceID = ma.InvoiceID
    ORDER BY [Timestamp]
    FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
    FROM invoices m
    GROUP BY InvoiceID
    ORDER BY InvoiceID;
    Here is the end result:
    InvoiceID InvoiceSteps
    0000210121_0002_2013
    Post FI Invoice,Clear Invoice
    My question: How can I improve the query so that it can process thousands of records as fast as possible (>600K in this case).
    Thanks you!

    There are many methods to concatenate the rows to columns. Assuming you have necessary indexes to support your query as Rishabh suggested, if you still find issues with performance, then you need to look at various other approaches as well. I have seen at
    certain places(huge data), CLR outperformed . Having said, we need to assess each and come to a conclusion for your scenario.
    Refer the below link for various approach, (please also look at the comment session as well):
    https://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/

  • Splitting comma seperated column data into multiple rows

    Hi Gurus,
    Please help me for solving below scenario. I have multiple value in single column with comma seperated values and my requirement is load that data into multiple rows.
    Below is the example:
    Source Data:
    Product         Size                                 Stock
    ABC              X,XL,XXL,M,L,S                 1,2,3,4,5,6
    Target Data:
    Product         Size                                 Stock
    ABC              X                                     1
    ABC              XL                                   2
    ABC              XXL                                 3
    ABC              M                                    4
    ABC              L                                      5
    ABC             S                                        6
    Which transformation we need to use for getting this output?
    Thanks in advance !

    Hello,
    Do you need to do this tranformation through OWB mapping only? And can you please tell what type of source you are using? Is it a flat file or a table?
    Thanks

  • Convert comma separated to string into multiple row

    Hi,
    I have SQL query which will fetch the data like below.
    {code}
    ID     NAME     CODE
    1     A,B          5,2
    2     X              10
    3     Z               4
    {code}
    So now i have to split the data like below
    {code}
    ID     NAME     CODE
    1     A               5
    1     B               2
    2     X              10
    3     Z               4
    {code}

    https://forums.oracle.com/thread/2549548

  • Converting single column content into rows

    Hi All,
    I have a table containing data in the following format
    SNO Content
    1 a,ab,aab,b,c
    2 a,aac,aab,c,ccb,ee
    3 bb,b,c
    I have a requirement to convert this into following format
    SNO Content
    1 a
    1 ab
    1 aab
    1 b
    1 c
    2 a
    3 bb
    3 b
    3 c
    How to acheive this in 10g? Is it possible in 9i?
    Please share your thoughts.
    Thanks in advance
    Regards,
    Subbu S

    test@ORA92>
    test@ORA92> @ver
    BANNER
    Oracle9i Enterprise Edition Release 9.2.0.1.0 - Production
    PL/SQL Release 9.2.0.1.0 - Production
    CORE    9.2.0.1.0       Production
    TNS for 32-bit Windows: Version 9.2.0.1.0 - Production
    NLSRTL Version 9.2.0.1.0 - Production
    5 rows selected.
    test@ORA92>
    test@ORA92> select * from t;
           SNO CONTENT
             1 a,ab,aab,b,c
             2 a,aac,aab,c,ccb,ee
             3 bb,b,c
    3 rows selected.
    test@ORA92>
    test@ORA92> select sno,
      2         rtrim(substr(lst,
      3                      instr(lst, ',', 1, iter.pos) + 1,
      4                      instr(lst, ',', 1, iter.pos + 1) - instr(lst, ',', 1, iter.pos)),
      5               ',') content
      6    from (select sno, ',' || content || ',' lst from t) csv,
      7         (select rownum pos from all_objects) iter
      8   where iter.pos <= ((length(csv.lst) - length(replace(csv.lst, ','))) / length(',')) - 1
      9   order by sno;
           SNO CONTENT
             1 a
             1 ab
             1 aab
             1 c
             1 b
             2 a
             2 aab
             2 ee
             2 ccb
             2 c
             2 aac
             3 bb
             3 c
             3 b
    14 rows selected.
    test@ORA92>
    test@ORA92>Haven't tried this with a large number of rows, but I suspect it would be slow in that case. Regular expressions should make this simpler and more efficient in 10g.
    Cheers,
    pratz

  • [b]Report, comma separated column data[/b]

    Hi all
    I M using Oracle 7.3 as backend and report 6i as frontend.
    I have a table T1 with columns
    custno - varchar2(4)
    billno - number(3)
    The data it contains as
    custno billno
    a123 100
    a123 200
    b234 111
    b234 222
    a123 300
    b234 333
    z678 765
    I want the output in report as follows
    custno billno
    a123 100,200,300
    b234 111,222,333
    z678 765
    I should mention here that the custno and bill no will keep on growing.
    Can anybody help
    UMESH

    Make a Group Left report using the Report wizard with the group field as CustNo and displayed fields as CustNo and Bill No.
    Now,
    For the Outermost repeating frame i.e. R_G_CustNo for the group G_CustNo,
    Select:
    Print Direction :Down
    Vertical Elasticity : Variable
    Horizontal Elasticity: Fixed
    With the other settings as default
    Now this repeating frame contains F_CustNo, R_G_BillNo, F_BillNo
    For the repeating frame i.e. R_G_ BillNo for the group G_ BillNo,
    Select:
    Print Direction : Across/Down
    Vertical Elasticity : Variable
    Horizontal Elasticity: Fixed
    With the other settings as default
    Please note that in the Paper Layout R_G_CustNo should be stretched horizontally over the whole panel whereas R_G_ BillNo should be just the minimum size required so that it can be repeated over R_G_CustNo horizontally
    Swati

  • Converting column data into rows in oracle 10g

    sample data:
    PATID NA2     NA3     NA4     
    1     3     4     5     
    1     34     45     56     
    1     134     245     356     
    2     134     245     356     
    2     334     275     56     
    2     4     275     56     
    2     4     5     56     
    how to display the above data like
    PATID NA2 NA3 NA4
    1 ID1 ID2 ID3 ID4 ID1 ID2 ID3 ID4 ID1 ID2 ID3 ID4
    3 34 134 4 45 245 5 56 356
    2 134 134 4 4 245 275 275 5 356 56 56 56

    Many examples are here:
    http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php
    (and you can do a search on this forum as well to find more)
    edit
    Your sample data is not very clear, by the way.
    Please post a CREATE TABLE and some INSERT statements, just enough to put up the testcase.
    Use the tag before and after posting examples, so formatting will be maintained.
    See the [FAQ|http://forums.oracle.com/forums/help.jspa] for more information regarding tags (scroll a bit down)...
    Edited by: hoek on Jan 26, 2010 5:23 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Query on column with comma separated values

    I have a proposed table with unnormalized data like the following:
    ID COLA COLB REFLIST
    21 xxx  zzz  24,25,78,412
    22 xxx  xxx  21
    24 yyy  xxx  912,22
    25 zzz  fff  433,555,22
    .. ...  ...  ...There are 200 million rows. There is maximum of about 10 IDs in the REFLIST, though typically two or three. How could I efficiently query this data on the REFLIST column? e.g. something like:
    SELECT id FROM mytable WHERE :myval in reflistLogically there is a many to many relationship between rows in this table. The REFLIST column contains pointers to ID values elsewhere in the table. The data could be normalized so that the relationship keys are in a separate table (in fact this is the current solution that we want to change).
    ID  REF
    21  24
    21  25
    21  78
    21  412
    22  21
    24  912
    ... ...The comma separated list seems instinctively like a bad idea, however there are various reasons for proposing it. The main reason is because the source for this data has it structured like the REFLIST example. It is an OLTP-like system rather than a data warehouse. The source code (and edit performance) would benefit greatly from not having to maintain the relationship table as the data changes.
    Going back to querying the REFLIST column, the problem seems to be building an approriate index for the data. The ideas proposed so far are:
    <li>Make a materialized view that presents the relationships as normalized (e.g. as in the example with ID, REF columns above), then index the plain column - the various methods of writing the view SQL have been widely posted.
    <li>Use a Oracle Text Index (not something I have ever had call to use before).
    Any other ideas? Its Oracle 10.2, though 11g could be possible.
    Thanks
    Jim

    Something like this ?
    This is test demo on my 11.2.0.1 Windows XP
    SQL> create table test (id number,reflist varchar2(30));
    Table created.
    SQL> insert into test values (21,'24,25,78,412');
    1 row created.
    SQL> insert into test values (22,'21');
    1 row created.
    SQL> insert into test values (24,'912,22');
    1 row created.
    SQL> insert into test values (25,'433,555,22');
    1 row created.
    SQL> select * from test
      2  where
      3  ',' || reflist || ',' like '%,22,%';
            ID REFLIST
            24 912,22
            25 433,555,22
    SQL>Source:http://stackoverflow.com/questions/7212282/is-it-possible-to-query-a-comma-separated-column-for-a-specific-value
    Regards
    Girish Sharma
    Edited by: Girish Sharma on Jul 12, 2012 2:31 PM

Maybe you are looking for