Rows to comma delimited string

Oracle version Release 10.2.0.4.0
i have the issue with the QuerySQL
SQL returning comma delimited rate_codes for each (outlet, id_service)
QuerySQL:
        select
            (select ltrim(MAX(SYS_CONNECT_BY_PATH(v.service,',')) KEEP (DENSE_RANK LAST ORDER BY curr),',') ratecodes
              from (
                     select os.outlet, rsm.id_service, os.service,
                            row_number() OVER (PARTITION BY os.outlet, rsm.id_service ORDER BY os.service) AS curr,
                            row_number() OVER (PARTITION BY os.outlet, rsm.id_service ORDER BY os.service) -1 AS prev
                       from prcsd_outletsvcs_test os,
                            ratecode_service_mapping_test rsm
                       where os.corp                = to_number(substr(:account_number,1,5))
                       and os.house                 = ltrim(substr(:account_number,6,6),'0')
                       and os.cust                  = ltrim(substr(:account_number,12,2),'0')
                       and os.outlet                = :no_outlet
                       and rsm.corp                 = os.corp
                       and rsm.rate_code            = os.service
                       and rsm.id_service           = :id_service
                    ) v
             group by outlet, id_service
            CONNECT BY prev = PRIOR curr
            START WITH curr = 1
            ) ratecodes
        from dual;parameter account_number 0787074919903, no_outlet=2, id_service=3 returns V1,XF
parameter account_number 0787074919903, no_outlet=2, id_service=1 returns X1
This sql works fine, but if i enclose it with another select ... dual and the QuerySQL in cursor like
( the parameters account_number, no_outlet and id_service is returned from some other tables in outer loop, here i am using outer dual to simulate my situation )
select
    :no_outlet, :id_service,
    cursor (
                QuerySQL above
            ) ratecodes
        from dual
    ) ratecodes
from dual;cursor output:
parameter account_number 0787074919903, no_outlet=2, id_service=3 returns V1,XF
parameter account_number 0787074919903, no_outlet=2, id_service=1 returns nothing
the (outlet,id_service) with only one rate_code is not returning anything
data creation:
CREATE TABLE PRCSD_OUTLETSVCS_TEST
  CORP     NUMBER                               NOT NULL,
  HOUSE    VARCHAR2(6 BYTE)                     NOT NULL,
  CUST     VARCHAR2(2 BYTE)                     NOT NULL,
  OUTLET   NUMBER                               NOT NULL,
  SERVICE  VARCHAR2(2 BYTE)                     NOT NULL
CREATE TABLE RATECODE_SERVICE_MAPPING_TEST
  CORP        NUMBER(5),
  ID_SERVICE  NUMBER(10),
  RATE_CODE   VARCHAR2(10 BYTE)
Insert into PRCSD_OUTLETSVCS_TEST(corp, house, cust, outlet, service)
Values(7870, '749199', '3', 1, 'X1');
Insert into PRCSD_OUTLETSVCS_TEST(corp, house, cust, outlet, service)
Values(7870, '749199', '3', 1, 'X2');
Insert into PRCSD_OUTLETSVCS_TEST(corp, house, cust, outlet, service)
Values(7870, '749199', '3', 1, 'XC');
Insert into PRCSD_OUTLETSVCS_TEST(corp, house, cust, outlet, service)
Values(7870, '749199', '3', 2, 'V1');
Insert into PRCSD_OUTLETSVCS_TEST(corp, house, cust, outlet, service)
Values(7870, '749199', '3', 2, 'X1');
Insert into PRCSD_OUTLETSVCS_TEST(corp, house, cust, outlet, service)
Values(7870, '749199', '3', 2, 'X2');
Insert into PRCSD_OUTLETSVCS_TEST(corp, house, cust, outlet, service)
Values(7870, '749199', '3', 2, 'XF');
Insert into PRCSD_OUTLETSVCS_TEST(corp, house, cust, outlet, service)
Values(7870, '749199', '3', 3, '94');
Insert into PRCSD_OUTLETSVCS_TEST(corp, house, cust, outlet, service)
Values(7870, '749199', '3', 3, 'X1');
Insert into PRCSD_OUTLETSVCS_TEST(corp, house, cust, outlet, service)
Values(7870, '749199', '3', 3, 'X2');
Insert into PRCSD_OUTLETSVCS_TEST(corp, house, cust, outlet, service)
Values(7870, '749199', '3', 3, 'XR');
Insert into RATECODE_SERVICE_MAPPING_TEST(corp, id_service, rate_code)
Values(7870, 1, 'X1');
Insert into RATECODE_SERVICE_MAPPING_TEST(corp, id_service, rate_code)
Values(7870, 2, 'XC');
Insert into RATECODE_SERVICE_MAPPING_TEST(corp, id_service, rate_code)
Values(7870, 3, 'V1');
Insert into RATECODE_SERVICE_MAPPING_TEST(corp, id_service, rate_code)
Values(7870, 3, 'XF');
Insert into RATECODE_SERVICE_MAPPING_TEST(corp, id_service, rate_code)
Values(7870, 4, '94');
Insert into RATECODE_SERVICE_MAPPING_TEST(corp, id_service, rate_code)
Values(7870, 4, 'X1');
Insert into RATECODE_SERVICE_MAPPING_TEST(corp, id_service, rate_code)
Values(7870, 4, 'XR');thanks for any help

pkchan wrote:
i added
where
v.outlet        = svc_rc.outlet
and v.id_service    = svc_rc.id_serviceand comment out the assignment lines with :no_outlet and :id_service
You misunderstand how hierarchical query works. Look at:
                     select  max(ltrim(SYS_CONNECT_BY_PATH(v.service,','),',')) ratecodes
                        from (
                              select  os.outlet,
                                      rsm.id_service,
                                      os.service,
                                      row_number() OVER (PARTITION BY os.outlet, rsm.id_service ORDER BY os.service) AS rn
                                from  prcsd_outletsvcs_test os,
                                      ratecode_service_mapping_test rsm
                                where os.corp        = to_number(substr(:account_number,1,5))
                                  and os.house       = ltrim(substr(:account_number,6,6),'0')
                                  and os.cust        = ltrim(substr(:account_number,12,2),'0')
                                  --and os.outlet      = :no_outlet
                                  and rsm.corp       = os.corp
                                  and rsm.rate_code  = os.service
                                  --and rsm.id_service = :id_service
                             ) v
                        where
                            v.outlet        = svc_rc.outlet
                        and v.id_service    = svc_rc.id_service
                            CONNECT BY rn = PRIOR rn + 1
                            START WITH rn = 1WHERE clause in hierarchical query is applied AFTER hierarchy is build. Therefore rn=1 from EACH combination is joined to rn=2 in ALL combinations and then joined to rn=3 in ALL combinations, etc. and only then WHERE clause is applied. To create proper hierarchies you need:
                        where
                            v.outlet        = svc_rc.outlet
                        and v.id_service    = svc_rc.id_service
                           CONNECT BY outlet = PRIOR outlet
                                  AND id_service = PRIOR id_service
                                 AND rn = PRIOR rn + 1
                            START WITH rn = 1Now:
with svc_rc as
    select distinct os.outlet, rsm.id_service
      from prcsd_outletsvcs_test os,
           ratecode_service_mapping_test rsm
     where rsm.corp       = os.corp
       and rsm.rate_code  = os.service
     order by outlet, id_service
select
    svc_rc.outlet, svc_rc.id_service,
    cursor (
            select  (
                     select  max(ltrim(SYS_CONNECT_BY_PATH(v.service,','),',')) ratecodes
                        from (
                              select  os.outlet,
                                      rsm.id_service,
                                      os.service,
                                      row_number() OVER (PARTITION BY os.outlet, rsm.id_service ORDER BY os.service) AS rn
                                from  prcsd_outletsvcs_test os,
                                      ratecode_service_mapping_test rsm
                                where os.corp        = to_number(substr(:account_number,1,5))
                                  and os.house       = ltrim(substr(:account_number,6,6),'0')
                                  and os.cust        = ltrim(substr(:account_number,12,2),'0')
                                  --and os.outlet      = :no_outlet
                                  and rsm.corp       = os.corp
                                  and rsm.rate_code  = os.service
                                  --and rsm.id_service = :id_service
                             ) v
                        where
                            v.outlet        = svc_rc.outlet
                        and v.id_service    = svc_rc.id_service
                           CONNECT BY outlet = PRIOR outlet
                                  AND id_service = PRIOR id_service
                                 AND rn = PRIOR rn + 1
                            START WITH rn = 1
                    ) ratecodes
              from  dual
           ) ratecodes
  from  svc_rc
    OUTLET ID_SERVICE RATECODES
         1          1 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
         1          2 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
         1          4 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
         2          1 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
         2          3 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
V1,XF
         2          4 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
         3          1 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
         3          4 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
94,X1,XR
8 rows selected.
SQL> You get 94,X1,XR, but still wrong results for other rows. So get rid of that extra level of select (we already established it is the source of the bug you are experiencing):
with svc_rc as
    select distinct os.outlet, rsm.id_service
      from prcsd_outletsvcs_test os,
           ratecode_service_mapping_test rsm
     where rsm.corp       = os.corp
       and rsm.rate_code  = os.service
     order by outlet, id_service
select
    svc_rc.outlet,
    svc_rc.id_service,
    cursor (
            select  ltrim(SYS_CONNECT_BY_PATH(v.service,','),',') ratecodes
              from (
                    select  os.outlet,
                            rsm.id_service,
                            os.service,
                            row_number() OVER (PARTITION BY os.outlet, rsm.id_service ORDER BY os.service) AS rn
                      from  prcsd_outletsvcs_test os,
                            ratecode_service_mapping_test rsm
                      where os.corp        = to_number(substr(:account_number,1,5))
                        and os.house       = ltrim(substr(:account_number,6,6),'0')
                        and os.cust        = ltrim(substr(:account_number,12,2),'0')
                        and rsm.corp       = os.corp
                        and rsm.rate_code  = os.service
                   ) v
              WHERE connect_by_isleaf = 1
                AND outlet = svc_rc.outlet
                AND id_service = svc_rc.id_service
              START WITH rn = 1
              CONNECT BY outlet = PRIOR outlet
                     AND id_service = PRIOR id_service
                     AND rn = PRIOR rn + 1
           ) ratecodes
  from  svc_rc
    OUTLET ID_SERVICE RATECODES
         1          1 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
X1
         1          2 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
XC
         1          4 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
X1
         2          1 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
X1
         2          3 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
V1,XF
         2          4 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
X1
         3          1 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
X1
         3          4 CURSOR STATEMENT : 3
CURSOR STATEMENT : 3
RATECODES
94,X1,XR
8 rows selected.
SQL> SY.

Similar Messages

  • Comma delimited string relating to subreports

    I previously opened a discussion called comma delimited string for a report header. The response I received then worked well for 'Year' but not for two other areas I am trying to use it for.
    Original Code
    Details:
    whileprintingrecords;
    stringvar Year:= Year+ {@Year} + ", ";
    Report Footer:
    whileprintingrecords;
    stringvar Year;
    Left(Year, len(Year)-2);
    I needed to modify the code to eliminate duplication in Year and this worked fine. See the code below. I also needed this code for two other areas, Author and School. Without the extra line of code with the 'instr' function the code would always show and error for ' Left(author, len(author)-2);'.  It came up with and error saying that the result was less than 0 or not and integer and went into debug mode. I added the instr function line and it worked for most but not all. This is my problem. Either the ' Left(author, len(author)-2);' line or 'If instr(author, {@Author } ) = 0 then' makes data disappear. It will show a comma delimited string for 'Author' on most rows but not from some. The same would be true for' School'. I am not sure what is going on. The code below is what I am currently using in the subreports.
    Details:
    whileprintingrecords;
    stringvar author;
    If instr(author, {@Author}) = 0 then
        author:= author + {@Author } + ", ";
    Report Footer:
    whileprintingrecords;
    stringvar author;
    If instr(author, {@Author } ) = 0 then
        author:= author + {@Author }  + ", ";
    Else
        Left(author, len(author)-2);

    Hi Abhilash,
    The code for the @Author is:
    If ({Command.chrStatus} = "External")then
        {Command.chrSurname} & ", " & {Command.chrFirstname}
    Else
        {Command.chrSurname(1)} & ", " & {Command.chrFirstname(1)}
    The goal of this code was to pull all the authors into one comma delimited string.
    eg Smith, Brian; Jones, Barry; Lee, Henry
    The same desire was with the Schools and the code was the same. I just don't know why this is returning the blanks.
    You mentioned in the last post that this would be better done in an SQL command. Should I open up another discussion because I think this would be the way to go. I just don't know how I can do the same comma delimited code in SQL.
    Thanks

  • Split Comma Delimited String Oracle

    I want to Split Comma Delimited string in oracle9i into rowwise data.i do not want to use Functions or connect by level...
    is there any other way from which we can split strings.???
    if we use Connect by level method the problem im facing is we cannot use suqueries in Connect by clause...
    Edited by: user11908943 on Sep 16, 2009 8:37 AM

    michaels2 wrote:
    I prefer using XMLTABLE which has superceded XMLSEQUENCE and I personally find it easier to read...Agree, but that would be hard in 9i since it is not implemented ;)Ah! missed that bit (gawd I wish people would upgrade to supported versions)
    In still later versions (11g) I prefer
    SQL>  with t as
    select 'a,b,c,d,e' str from dual
    select trim(column_value) str
    from t, xmltable(('"' || replace(str, ',', '","') || '"'))
    STR                                                                            
    a                                                                              
    b                                                                              
    c                                                                              
    d                                                                              
    e                                                                              
    5 rows selected.
    Natty! But completely illogical (I mean creating an XMLTABLE from a non-XML string). I'm guessing that's a development of XQuery?

  • Using a comma-delimited string in Dynamic SQL

    Hi --
    If I receive a comma-delimited string as an in parameter, can I simply use that (in string format) when building my dynamic sql?
    Thanks,
    Christine

    The problem is, that you can not use bind variables
    here, only literals. This causes
    eventual performance problems.And to avoid the inevitable database performance problems Dmytro mentions you can use a function to convert the string to a varray and select from that. This also avoids having to use dynamic sql.
    First you create a varray and conversion function.
    SQL> create or replace type tabstr_t as table of varchar2(255)
      2  /
    Type created.
    SQL> create or replace function tabstr (
      2      p_str in varchar2,
      3      p_sep in varchar2 default ','
      4      )
      5  return tabstr_t
      6  as
      7      l_str long default p_str || p_sep;
      8      l_tabstr tabstr_t := tabstr_t();
      9  begin
    10      while l_str is not null loop
    11          l_tabstr.extend(1);
    12          l_tabstr(l_tabstr.count) := rtrim(substr(
    13                  l_str,1,instr(l_str,p_sep)),p_sep);
    14          l_str := substr(l_str,instr(l_str,p_sep)+1);
    15      end loop;
    16      return l_tabstr;
    17  end;
    18  /
    Function created.Then you can use these in either regular sql.
    SQL> var s varchar2(100)
    SQL> exec :s := 'Smith,Scott,Miller'
    PL/SQL procedure successfully completed.
    SQL>
    SQL> select * from emp where ename in
      2      (select upper(column_value) from table(tabstr(:s)));
    EMPNO ENAME    JOB          MGR HIREDATE     SAL   COMM  DEPTNO
      7369 SMITH    CLERK       7902 17-DEC-80    800             20
      7788 SCOTT    ANALYST     7566 09-DEC-82   3000             20
      7934 MILLER   CLERK       7782 23-JAN-82   1300             10Or in pl/sql.
    SQL> var c refcursor
    SQL> begin
      2      open :c for
      3      select * from emp where ename in
      4          (select upper(column_value) from table(tabstr(:s)));
      5  end;
      6  /
    PL/SQL procedure successfully completed.
    SQL> print c
    EMPNO ENAME    JOB          MGR HIREDATE     SAL   COMM  DEPTNO
      7369 SMITH    CLERK       7902 17-DEC-80    800             20
      7788 SCOTT    ANALYST     7566 09-DEC-82   3000             20
      7934 MILLER   CLERK       7782 23-JAN-82   1300             10

  • Converting Floats to comma-delimited strings?

    Does anyone know of any methods or classes that can convert a float to a comma delimited string?
    Alternatively, conversion of a non-comma delimited string to a comma-delimited string would be just as good.
    An example of a comma-delimited string would be the display on the iPhone calculator.
    -- tia rick

    Also in this guide:
    https://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/DataFo rmatting/DataFormatting.html
    and a small example:
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    //[numberFormatter setFormat:@"$#,##0;$#,##0;-$#,##0"];
    label.text = [numberFormatter stringFromNumber:[NSNumber numberWithFloat:floatVal]];
    [numberFormatter release];
    notice the commented out line is commented out because apparently setFormat is not supported on the iPhone (yet???) It's what I really need.
    good luck

  • DRM Function to divide comma delimited string to list value

    Hi
    Any one knows how to seperate comma delimited string into list values.
    Thanks

    Are you wanting to dynamically set the list values for a property? If so there are a couple options:
    1. You can update them via the API.
    2. You can write a sql script to update the table Property_List. This requires an application server restart and generally isn't recommended for support reasons but it makes sense in some instances.
    Edited by: Naren Truelove on 16/02/2011 22:08

  • How do I split a comma delimited string into an array.

    I have a string that I am passing into a function that is Comma delimited. I want to split the string into an array. Is there a way to do this.
    Thanks in advance.

    trouble confirmed on 10gR1 and 10gR2
    works with 'a,b,c'  and also with  '  "1"  , "2"  ,  "3"  '
    does not work with '1,2,3' 
    throwing ORA-6512                                                                                                                                                                                                                                                                                                                           

  • Splitting of comma delimited string CONNECT BY Clause

    Hi ,
    I have got a problem in splitting a comma separated strings into rows .
    I am explaining the use case below :
    i have a table x_imp
    --> create table x_imp (id number, int_status varchar2(100),c_ref varchar2(30), s_ref varchar2(30));
    I inserted values into the table :
    insert into x_imp (id, int_status,c_ref,s_ref) values (1,'a1,a2,a3,a4','A','AS');
    insert into x_imp (id, int_status,c_ref,s_ref) values (1,'b1,b2,b3,b4','B', 'BS');
    insert into x_imp (id, int_status,c_ref,s_ref) values (1,'c1,c2,c3,c4', 'C', null);
    insert into x_imp (id, int_status, cust_ref, site_ref) values (1,NULL, 'D', NULL);
    I need to split the comma separated int_status into individual rows . That means my expected result is :
    . What I need or looking for as expected result:
    1, A, AS, a1
    1, A, AS, a2
    1, A, AS, a3
    1, A, AS, a4
    1, B, BS, b1
    1, B, BS, b2
    1, B, BS, b3
    1, B, BS, b4
    1, C, null, c1
    1, C, null, c2
    1, C, null, c3
    1, C, null, c4
    I currently have a solution using Regex . But this solution uses UNIQUE keyword .
    The solution i have currently :
    select UNIQUE c_ref,s_ref, regexp_substr(int_status,'[^,]+', 1, level) error_code
    from x_imp
    connect by regexp_substr(int_status, '[^,]+', 1, level) is not null;
    I need a better solution . Any pointers ?
    Thanks,
    Bibin

    One way of doing using MODEL clause:
    select c_ref, s_ref, res err_code
      FROM x_imp
    model
    partition by ( id, c_ref, s_ref)
    dimension by ( 1 rn)
    measures( int_status, cast(null as varchar2(100)) res,
              nvl(length(regexp_replace(int_status, '[^,]')),0)+1 nb )
    (res[for rn from 1 to nb[1] increment 1]=regexp_substr(int_status[1], '[^,]+', 1, cv(rn)))
    order by c_ref, s_ref;
    C_REF                          S_REF                          ERR_CODE                                                                                            
    A                              AS                             a3                                                                                                  
    A                              AS                             a4                                                                                                  
    A                              AS                             a1                                                                                                  
    A                              AS                             a2                                                                                                  
    B                              BS                             b2                                                                                                  
    B                              BS                             b1                                                                                                  
    B                              BS                             b3                                                                                                  
    B                              BS                             b4                                                                                                  
    C                                                             c4                                                                                                  
    C                                                             c3                                                                                                  
    C                                                             c1                                                                                                  
    C                                                             c2                                                                                                  
    D                                                                                                                                                                 
    13 rows selected

  • Comma Delimited String

    Hi Gurus,
    I have a problem:
    The following string has commas I want to break it as:
    'ABC,DEFGHI,DEE,YYY,XXX,333'
    'ABC'
    'DEFGHI'
    'DEE'
    'YYY'
    'XXX'
    '333'Any help will be highly appreciated.
    Thanks
    Aqil

    SQL> select * from v$version where rownum=1;
    BANNER
    Oracle8i Enterprise Edition Release 8.1.7.4.1 - Production
    SQL> select substr(col,instr(col,',',1,rownum)+1,instr(col,',',1,rownum+1)-instr(col,',',1,rownum)-1)
      2  from (select ','||'ABC,DEFGHI,DEE,YYY,XXX,333'||',' col from dual) a, all_objects b
      3  where rownum <= length(col)-length(replace(col,','))-1;
    SUBSTR(COL,INSTR(COL,',',1,R
    ABC
    DEFGHI
    DEE
    YYY
    XXX
    333
    6 rows selected.
    SQL> Nicolas.

  • How to split comma delimited string?

    Hi,
    I have a parameter called Id in my SP which will be of nvarchar data type and i'm going to get the multiple ids at a time seperated by commas in that parameter from the application. Now my requirement is to update a table with these comma seperated ids in seperate rows.
    For example, if i have 2 parameters called Id1 and Id2. Id1 will contain only one value and will be of int data type and Id2 will be of nvarchar data type as i can get multiple ids delimited by a comma from the application. Suppose Id1 = 1 and Id2 = '1,2,3,4'. Then I have to update id2 in the tables seperately like wherever Id1 is '1' i need to update Id2 column for 4 rows with the value 1, 2, 3, 4 respectively in different rows.
    Could you please tell me how can i do this in T-SQL? How can i split the data of parameter Id2 in 4 different rows?
    Please let me know if you did not understand the question.
    Thanks,
    Deepti

     deeptideshpande wrote:
    Hi,
    I got a similar function which is like this:
    CREATE FUNCTION dbo.fnSplit(
    @sInputList VARCHAR(8000) -- List of delimited items
    , @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
    ) RETURNS @List TABLE (item VARCHAR(8000))
    BEGIN
    DECLARE @sItem VARCHAR(8000)
    WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
    BEGIN
    SELECT
    @sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,@sInputList,0)-1))),
    @sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,@sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
    IF LEN(@sItem) > 0
    INSERT INTO @List SELECT @sItem
    END
    IF LEN(@sInputList) > 0
    INSERT INTO @List SELECT @sInputList -- Put the last item in
    RETURN
    END
    GO
    Now when i use this in my select statement, i get data in a tabular structure as shown below:
    select
    from dbo.fnSplit('1,22,333,444,,5555,666',
    Results
    Item
    1
    22
    333
    444
    5555
    666
    Now my requirement is to insert this value in a table which has 2 columns ID1 and ID2.
    At any point of time ID1 will be an int value(only one id in that parameter) and ID2 can have multiple ids delimited by comma. With the help of above split function i can split values of ID2 into 6 different ids but i need to insert the table
    in the following way:
    ID1     ID2
    1        1
    1        22
    1        333
    1        444
    1        5555
    1        666
    How can i insert the data in the above mentioned fashion.
    Thanks,
    Deepti
    Like this:
    Code Snippet
    select 1 as id,* from dbo.fnSplit('1,22,333,444,,5555,666', ',')
    Great!!!! Wonderfull!!!... Thanks a lot.

  • Combining rows with comma delimited

    Hi All,
    I have two tables
    Table A
    ID Location ID
    1 6
    2 7
    1 9
    2 10
    3 7
    1 8
    2 9
    Table B
    ID
    1
    2
    3
    49
    8
    6
    I have to come up with something like this for each ID in table B, I have to write a query to get
    ID Location ID
    1 6, 9, 8
    2 7, 10, 9
    so for each Id I need to grab there Location Id and concatenate them with comma.
    How can I acheive this. Is it possible to avoid loops in this case?
    Anye help will be apprecaited.

    with a as (
               select 1 id,6 location_id from dual union all
               select 2,7 from dual union all
               select 1,9 from dual union all
               select 2,10 from dual union all
               select 3,7 from dual union all
               select 1,8 from dual union all
               select 2,9 from dual
         b as (
               select 1 id from dual union all
               select 2 from dual union all
               select 3 from dual union all
               select 49 from dual union all
               select 8 from dual union all
               select 6 from dual
    -- end of on-the-fly data sample
    select  id,
            rtrim(xmlagg(xmlelement(e,location_id,',').extract('//text()') order by rn),',') location_id_list
      from  (
             select  a.id,
                     a.location_id,
                     row_number() over(partition by a.id order by a.location_id) rn
               from  a,
                     b
               where b.id = a.id
      group by id
            ID LOCATION_ID_LIST
             1 6,8,9
             2 7,9,10
             3 7
    3 rows selected.
    SQL> SY.

  • Using a comma seprated string as rows

    i hv a column in which i store comma seprated name, i have to show them as rows in a report..
    plz help

    As with most things, it depends greatly on your Oracle version (you should always post this information).
    In release 11 here's a neat method.
    http://laurentschneider.com/wordpress/2009/07/select-from-column-separated-list.html
    In release 10 there's REGEXP
    http://nuijten.blogspot.com/2009/07/splitting-comma-delimited-string-regexp.html
    And it looks like you've been given a pre-10 answer already.

  • Inserting the Comma Separated Strings into Table

    Hi Seniors,
    i had two string and i want to insert the records in the Table COMMENT . In this way.
    would u please give some programe to insert the records.
    The Data and the Table
    ( 901,902,903,904 )
    ( 'hai','nice','good & mail is [email protected] ','excellent and the phone 011-235323' )
    comm_id loc_id company_name comments
    1      10 901      Hai
    2      10 902      nice
    3 10      903      good & mail is [email protected]
    4      10 904      excellent and the phone 011-235323
    Thanks
    Seenu

    Hi, Seenu,
    In Oracle 10 (and up) you can easily split a comma-delimited list using REGEXP_SUBSTR.
    INSTR and SUBSTR can do the same thing in any version, but it's more complicated.
    See the general instructions below:
    /*     How to Split a Delimited String
    This shows how to take a single row with a delimited string, such as
         Animal     amoeba,bat,cedusa,dodo
    and transform it into multiple rows:
         Animal     1     amoeba
         Animal     2     bat
         Animal     3     cedusa
         Animal     4     dodo
    PROMPT     ==========  -1. sep_char parameter  ==========
    VARIABLE     sep_char     VARCHAR2 (10)
    EXECUTE     :sep_char := ',';
    SELECT     :sep_char     AS sep_char
    FROM     dual;
    PROMPT     ==========  0. string_test table  ==========
    DROP TABLE     string_test;
    CREATE TABLE     string_test
    (     grp_name     VARCHAR2 (10)
    ,     list_txt     VARCHAR2 (50)
    INSERT INTO string_test (grp_name, list_txt) VALUES ('Animal',     'amoeba,bat,cedusa,dodo');
    INSERT INTO string_test (grp_name, list_txt) VALUES ('Date',     '15-Oct-1582,16-Oct-2008');
    INSERT INTO string_test (grp_name, list_txt) VALUES ('Nothing',     NULL);
    INSERT INTO string_test (grp_name, list_txt) VALUES ('Place',     'New York');
    INSERT INTO string_test (grp_name, list_txt) VALUES ('Skip',     'Hop,,Jump');
    SELECT     *
    FROM     string_test
    ORDER BY     grp_name;
    PROMPT     ==========  Q1.  Oracle 11 Query  ==========
    WITH     cntr     AS          -- Requires Oracle 9
    (     -- Begin sub-query cntr, to generate n (1, 2, 3, ...)
         SELECT     LEVEL     AS n     -- Requires Oracle 9
         FROM     dual
         CONNECT BY     LEVEL     <= 1 +     (
                             SELECT     MAX ( REGEXP_COUNT (list_txt, :sep_char) )     -- Requires Oracle 11
                             FROM     string_test
    )     -- End sub-query cntr, to generate n (1, 2, 3, ...)
    SELECT     grp_name
    ,     n
    ,     REGEXP_SUBSTR     ( list_txt     -- Requires Oracle 10
                   , '[^' || :sep_char || ']'     -- Anything except sep_char ...
                        || '+'               -- ... one or more times
                   , 1
                   , n
                   )     AS item_txt
    FROM     string_test
    JOIN     cntr                                   -- Requires Oracle 9
    ON     n     <= 1 + REGEXP_COUNT (list_txt, :sep_char)     -- Requires Oracle 11
    ORDER BY     grp_name
    ,          n;
    /*     Notes:
         REGEXP_SUBSTR (s, '[^,]+', 1, n)
    returns the n-th item in a comma-delimited list s.
    If there are fewer than n items, it returns NULL.
    One or more consecutive characters other than comma make an item, so
    'Hop,,Jump' has two items, the second one being 'Jump'.
    The sub-query cntr produces a list of integers 1, 2, 3, ..., w
    where w is the worst-case (the largest number of items in any list).
    This actually counts separators, not items, (e.g., it counts both
    commas in 'Hop,,Jump', even though), so the w it produces may be
    larger than is really necessary.  No real harm is done.
    PROMPT     ==========  Q2. Possible Problems Fixed  ==========
    WITH     cntr     AS
    (     -- Begin sub-query cntr, to generate n (1, 2, 3, ...)
         SELECT     LEVEL     AS n
         FROM     dual
         CONNECT BY     LEVEL     <= 1 +     (
                             SELECT     MAX ( REGEXP_COUNT (list_txt, :sep_char) )
                             FROM     string_test
    )     -- End sub-query cntr, to generate n (1, 2, 3, ...)
    SELECT     grp_name
    ,     n
    ,     REGEXP_SUBSTR     ( list_txt
                   , '[^' || :sep_char || ']'     -- Anything except sep_char ...
                        || '+'               -- ... one or more times
                   , 1
                   , n
                   )     AS item_txt
    FROM     string_test
    JOIN     cntr          ON n     <= 1 + NVL     ( REGEXP_COUNT (list_txt, :sep_char)     -- Problem (1)
                                  , 0
    WHERE     REGEXP_SUBSTR     ( list_txt     -- Problem (2)
                   , '[^' || :sep_char || ']'     -- Anything except sep_char ...
                        || '+'               -- ... one or more times
                   , 1
                   , n
                   )     IS NOT NULL
    OR     list_txt     IS NULL          -- Problems (1) and (2) together
    ORDER BY     grp_name
    ,          n;
         (Possible) Problems and Fixes
    (1) If list_txt IS NULL, then REGEXP_COUNT (list_txt, :sep_char)
         returns NULL, the join condition fails, and the output
         contains nothing corresponding to the row from string_test.
         If you want a NULL item to appear in the results, use
         NVL to make sure the expression returns 0 instead of NULL.
    (2) If list_txt contains multiple consecutive sep_chars (or if it
         begins or ends with sep_char, then the original query
         will return NULL items.  To suppress these, add a WHERE
         clause to test that the item_txt to be displayed IS NOT NULL.
    PROMPT     ==========  Q3. Oracle 8.1 Query  ===========
    SELECT     grp_name
    ,     n
    ,     SUBSTR     ( list_txt
              , begin_pos
              , end_pos - begin_pos
              )     AS item_txt
    FROM     (     -- Begin sub-query to compute begin_pos and end_pos
         SELECT     grp_name
         ,     n
         ,     list_txt
         ,     INSTR     ( :sep_char || list_txt
                   , :sep_char
                   , 1
                   , n
                   )     AS begin_pos
         ,     INSTR     ( list_txt || :sep_char
                   , :sep_char
                   , 1
                   , n
                   )     AS end_pos
         FROM     string_test
         ,     (     -- Begin sub-query cntr, to generate n (1, 2, 3, ...)
              SELECT     ROWNUM     AS n
              FROM     all_objects
              WHERE     ROWNUM     <= 1 +     (
                             SELECT     MAX     ( LENGTH (list_txt)
                                       - LENGTH (REPLACE (list_txt, :sep_char))
                             FROM     string_test
              )     -- End sub-query cntr, to generate n (1, 2, 3, ...)
              cntr
         WHERE     n     <= 1 +     ( LENGTH (list_txt)
                        - LENGTH (REPLACE (list_txt, :sep_char))
         )     -- End sub-query to compute begin_pos and end_pos
    ORDER BY     grp_name
    ,          n;
    /*     Version-Dependent Features and Work-Arounds
    The code above, Q3, runs in Oracle 8.1.
    The following changes were made to Q1:
    (11) REGEXP_COUNT was introduced in Oracle 11.
         In earlier versions, to find the number of sep_chars in list_txt,
         see how much the LENGTH changes when sep_chars are removed.
    (10) REGEXP_SUBSTR was introduced in Oracle 10.
         In earlier versions, use INSTR to find where the sep_chars are,
         and use SUBSTR to get the sub-strings between them.
         (Using this technique, 'Hop,,Jump' still contains three items,
         but now item 2 IS NULL and item 3 is 'Jump'.)
    (9.a) The WITH-clause was introduced in Oracle 9
         In earlier versions, use in-line views.
    (9.b) "CONNECT BY LEVEL < constant" doesn't work in Oracle 8.
         Use ROWNUM from any sufficiently large table or view instead.
    (9.c) ANSII join notation (JOIN table_name ON ...) was introduced in Oracle 9
         In earlier versions, join condition go in a WHERE-clause.
    */

  • Comma delimited formatting in Smartview

    I'm creating a report in smartview 9.3.1
    I have few columns with formulas and I have format my report (bold ,comma delimited and some color).
    The issue is when I do a refresh all the formatting remains intact except the comma delimited formatting on columns with formulas.
    I already have the "Use Excel Formatting" checked.
    Does any one run into this issue before ?

    Hi Brian,
    There are a few ways to do this:
    Using Subreports:
    1) Insert a Subreport with its datasource being the same table as the Main Report's in the Report Header.
    2) Inside the Subreport, create a formula with a code that looks similar to the below:
    whileprintingrecords;
    stringvar dates := dates + {Year} + ", ";
    Place the formula on the Details section
    3) Create another formula and place it on the Report Footer:
    whileprintingrecords;
    stringvar dates;
    Left(dates, len(dates)-2);
    4) Suppress all the sections of the Subreport, except the Report Footer
    5) Copy the record selection formula from the Main Report into the Subreport so that it returns the same records as the Subreport
    If the data for 'Authors' and 'Departments' is available for each row just like the 'Year', you can actually use the same subreport and modify the above formula by adding 'shared variables' to bring back a comma delimited string of the 'years', 'authors' and 'departments'
    Using SQL:
    This is probably the most efficient way to do this. Just write a SQL that returns the list of years as a comma separated string along with the other columns > drag and drop the column on the report header
    -Abhilash

  • Error in create-nodeset-from-delimited-string

    Hi Gurus,
    I am trying to use the orcl:create-nodeset-from-delimited-string XPath extension function in the XSL mapper.
    I am using the Map test feature to test my map, and notice that if I use this function, the XSL does not give any output - i guess the map is failing to execute at runtime. When I open the JDeveloper as jdev.exe [so that I can see the command line window for JDev], I can see that an error appears in the command line window that states "Method orcl:create-nodeset-from-delimited-string" not found.
    This XPath function is available for use in the "Advanced Functions" palette in the XSL Mapper.
    Please let me know if
    a) This function can be used within the Mapper, and if so,
    b) whats the right way to use it.
    My usage looks like
    orcl:create-nodeset-from-delimited-string('tns:sampeNode',$delimVar,',')
    where delimVar is a comma delimited string.
    Please let me know your thoughts on this .
    rgds,
    Ram

    Hello..
    Look, I paste a functional piece of code that I develop in a transformation.
    Take this for your example and develop with the same structure.
    BR...Rodrigo
    <xsl:param name="HomeNumber"
    select="/tns:GetCustomerResponse/tns:GetCustomerResult/tns:DefaultAddress/tns:HomePhone"/>
    <xsl:param name="HomeNumberExt"
    select="/tns:GetCustomerResponse/tns:GetCustomerResult/tns:DefaultAddress/tns:HomePhoneExt"/>
    <xsl:param name="WorkNumber"
    select="/tns:GetCustomerResponse/tns:GetCustomerResult/tns:DefaultAddress/tns:WorkPhone"/>
    <xsl:param name="WorkNumberExt"
    select="/tns:GetCustomerResponse/tns:GetCustomerResult/tns:DefaultAddress/tns:WorkPhoneExt"/>
    <xsl:param name="Fax1"
    select="/tns:GetCustomerResponse/tns:GetCustomerResult/tns:DefaultAddress/tns:Fax1"/>
    <xsl:param name="Fax2"
    select="/tns:GetCustomerResponse/tns:GetCustomerResult/tns:DefaultAddress/tns:Fax2"/>
    <xsl:param name="Email"
    select="/tns:GetCustomerResponse/tns:GetCustomerResult/tns:DefaultAddress/tns:Email"/>
    <xsl:param name="contactMedium"
    select="concat($HomeNumber,'-%',$HomeNumberExt,'-%',$WorkNumber,'-%',$WorkNumberExt,'-%',$Fax1,'-%',$Fax2,'-%',$Email,'-')"/>
    <xsl:param name="contactMediumNodeSet"
    select="oraext:create-nodeset-from-delimited-string('contactMediumNodeSet',$contactMedium,'%')"/>

Maybe you are looking for