How to split columns into rows

Hi All,
Below is my table structure:=
SQL> create table split(id number,value varchar2(200));
Table created.
SQL> insert into split values(1,'X,Y,Z');
1 row created.
SQL> insert into split values(2,'A,B,C');
1 row created.
SQL> commit;
Commit complete.
Expected Output
ID Value
1 X
1 Y
1 Z
2 A
2 B
3 C
I know the feature of converting rows into columns by listagg in Oracle 11g, but is there any feature to convert rows into columns based on a delemiter..."," in my case.
Please help....
Thanks
Arijit

>
is there any feature to convert rows into columns based on a delemiter
>
Here is one way
VAR csv VARCHAR2(100)EXEC :csv := 'abc,de,fg,hij,klmn,o,pq,rst,uvw,xyz';
The query:
WITH data AS( SELECT SUBSTR(csv, INSTR(csv,',',1,LEVEL)+1,                     INSTR(csv,',',1,LEVEL+1) - INSTR(csv,',',1,LEVEL)-1 ) token    FROM ( SELECT ','||:csv||',' csv FROM SYS.DUAL ) CONNECT BY LEVEL < LENGTH(:csv)-LENGTH(REPLACE(:csv,',',''))+2 )SELECT token  FROM data;See http://projectwownow.blogspot.com/2010/02/oracle-convert-csv-string-into-rows.html

Similar Messages

  • How to turn columns into rows

    Hi. Does anyone know how to turn columns into rows ie:
    select field1, field2, field3, field4, field5 from table
    desired result:
    field1 field2
    field1 field3
    field1 field4
    field1 field5
    Thank you!

    Something like this ?
    select field1
    , case n.l
    when 1 then field2
    when 2 then field3
    when 3 then field4
    when 4 then field5
    end field
    from table
    , (select level l from dual connect by level <= 4) n

  • How to convert columns into rows using  transpose function

    Hi
    anybody tell me how to convert columns values into rows using transpose function.

    Since BluShadow went to all the trouble to put it together, someone should use it.
    See the post titled How do I convert rows to columns? here SQL and PL/SQL FAQ
    John

  • How to convert  columns into rows

    present result
    Mat_num        comp_code   disc          amount     
    800000     SG01      SAPLF005                    0.00
    800000     SG01      SAPLF005               31,500.00
    6300001    SPM       SAPLF005                    0.00
    6300001    SPM       SAPLF005              108,888.00
    APS100     SMTP      SAPLF005                    0.00
    APS100     SMTP      SAPLF005                    0.00
    required format
    Mat_num       disc             SG01          SMTP          SPM
    800000   SAPLF005   31,500.00           0.00          0.00
    800000   SAPLF005        0.00           0.00          0.00 
    6300001  SAPLF005        0.00         0.00     108,888.00     
    6300001  SAPLF005        0.00         0.00           0.00  
    APS100   SAPLF005        0.00         0.00           0.00 
    APS100   SAPLF005        0.00         0.00           0.00
    Plain Text Attachment [ Scan and Save to Computer ]
    REPORT  Z_CALC2                                 .
    TABLES : KNC1.
    TYPE-POOLS: SLIS.
      type-pools : abap.
    field-symbols: <dyn_table> type standard table,
                   <dyn_wa>,
                   <dyn_field>.
    data: dy_table type ref to data,
          dy_line  type ref to data,
          xfc type lvc_s_fcat,
          ifc type lvc_t_fcat.
    DATA: BEGIN OF ITAB OCCURS 0,
              KUNNR LIKE KNC1-KUNNR,
              BUKRS LIKE KNC1-BUKRS,
              GJAHR LIKE KNC1-GJAHR,
              USNAM LIKE KNC1-USNAM,
              UMSAV LIKE KNC1-UMSAV,
          END OF ITAB.
    *Result table
    TYPES BEGIN OF RES_ITAB.
    * RES_ITAB(10) TYPE C DEFAULE 'ZRES_ITAB'.
    INCLUDE STRUCTURE ZRES_ITAB.
    TYPES END OF RES_ITAB.
    SELECT-OPTIONS : CCODE FOR KNC1-BUKRS DEFAULT 'SG01' TO 'SPM '.
    DATA : BEGIN OF COMP_ITAB OCCURS 0,
              BUKRS LIKE KNC1-BUKRS,
           END OF COMP_ITAB.
    DATA: COLS TYPE I,
          gap(10) TYPE c,
          Company_title type string .
    start-of-selection.
      perform get_NUM_COLS.
      perform get_structure.
      perform create_dynamic_itab.
      perform get_data.
    *  perform write_out.
    FORM GET_NUM_COLS.
    SELECT KUNNR BUKRS GJAHR USNAM UMSAV INTO TABLE ITAB FROM KNC1 WHERE
    BUKRS IN CCODE.
    CLEAR ITAB.
    *CLEAR RES_ITAB.
    LOOP AT ITAB .
         COMP_ITAB-BUKRS = ITAB-BUKRS.
         APPEND COMP_ITAB.
    CLEAR COMP_ITAB.
    ENDLOOP.
    SORT COMP_ITAB BY BUKRS.
    DELETE ADJACENT DUPLICATES FROM COMP_ITAB.
    DESCRIBE TABLE COMP_ITAB LINES COLS.
    ENDFORM.
    *Get table structure
    form get_structure.
    data : idetails type abap_compdescr_tab,
           xdetails type abap_compdescr.
    data : ref_table_des type ref to cl_abap_structdescr.
    data fld_pos type i value 1.
    * Get the structure of the table.
      ref_table_des ?=
                  cl_abap_typedescr=>describe_by_name( 'ZRES_ITAB' ).
      idetails[] = ref_table_des->components[].
      loop at idetails into xdetails.
        clear xfc.
        XFC-COL_POS = fld_pos.
        xfc-fieldname = xdetails-name .
        xfc-datatype = xdetails-type_kind.
        xfc-inttype = xdetails-type_kind.
        xfc-intlen = xdetails-length.
        xfc-decimals = xdetails-decimals.
        append xfc to ifc.
        fld_pos = fld_pos + 1.
      endloop.
      DO  COLS TIMES.
        clear xfc.
        READ TABLE COMP_ITAB INDEX SY-INDEX.
        XFC-COL_POS = fld_pos.
        xfc-fieldname = COMP_ITAB-BUKRS .
        xfc-datatype = 'F'.
    *    xfc-inttype = 'F'.   'details-type_kind.
        xfc-intlen = 14.
    *    xfc-decimals = xdetails-decimals.
        append xfc to ifc.
        fld_pos = fld_pos + 1.
      ENDDO.
      clear xfc.
        XFC-COL_POS = fld_pos.
        xfc-fieldname = 'RECORD_TOTAL' .
        xfc-datatype = 'F'.
        xfc-inttype  = 'F'.
        xfc-intlen = 14.
    *    xfc-decimals = xdetails-decimals.
        append xfc to ifc.
    endform.
    form create_dynamic_itab.
    * Create dynamic internal table and assign to FS
      call method cl_alv_table_create=>create_dynamic_table
                   exporting
                      it_fieldcatalog = ifc
                   importing
                      ep_table        = dy_table.
      assign dy_table->* to <dyn_table>.
    * Create dynamic work area and assign to FS
      create data dy_line like line of <dyn_table>.
      assign dy_line->* to <dyn_wa>.
    endform.
    form get_data.
    LOOP AT ITAB.
            <DYN_WA>-KUNNR = ITAB-KUNNR.
            <DYN_WA>-USNAM = ITAB-USNAM.
            DO COLS TIMES.
               IF <DYN_WA>
    DESCRIBE TABLE <dyn_table>.
    *WRITE:/ SY-TFILL.
    * Select Data from table.
    * <dyn_table> = ITAB[].
    *LOOP AT ITAB.
    *    RES-KUNNR = ITAB-KUNNR.
    *    RES-USNAM = ITAB-USNAM.
    *    CASE ITAB-GJAHR.
    *        WHEN 2005.
    *            RES-UMSAV1 = ITAB-UMSAV.
    *        WHEN 2006.
    *            RES-UMSAV2 = ITAB-UMSAV.
    *        WHEN 2007.
    *            RES-UMSAV3 = ITAB-UMSAV.
    *    ENDCASE.
    *    RES-TOTAL = RES-UMSAV1 + RES-UMSAV2 + RES-UMSAV3 .
    * APPEND RES.
    *ENDLOOP.
    endform.
    form write_out.
      loop at <dyn_table> into <dyn_wa>.
        do.
          assign component  sy-index
             of structure <dyn_wa> to <dyn_field>.
          if sy-subrc <> 0.
            exit.
          endif.
          if sy-index = 1.
            write:/ <dyn_field>.
          else.
            write: <dyn_field>.
          endif.
        enddo.
      endloop.
    endform.
    Edited by: Alvaro Tejada Galindo on Feb 28, 2008 11:56 AM

    solved myself

  • How to display the rows in to columns and columns into rows?

    DES:- I know by using pivot and unpivot you can convert rows into columns and columns into rows but i don't know how to write the syntax?

    Hi,
    Besides the places Martin mentioned above, there are many examples in the forum FAQ:
    https://community.oracle.com/message/9362005#9362005
    For an example of both PIVOT and UNPIVOT in the same query, see
    http://forums.oracle.com/forums/thread.jspa?threadID=920854&tstart=0

  • One Column into Rows

    I have data in a table that looks like below:
    ColumnA               ColumnB
    123                    abc|cde|fgr
    345                    def|ght|sew
    890                    deq|nmk|lop|lip|fre|dwsThere is no limit on how many values you can have in ColumnB and they are pipe delimited.
    I need to split this one column into rows as:
    ColumnA               ColumnB
    123                    abc
    123                    cde
    123                    fgr
    345                    def
    345                    ght
    890                    fre
    890                    dwsThanks in advance!

    with sample_data as (
                         select 123 columna,'abc|cde|fgr' columnb from dual union all
                         select 345,'def|ght|sew' from dual union all
                         select 890,'deq|nmk|lop|lip|fre|dws' from dual
    select  columna,
            regexp_substr(columnb,'[^|]+',1,column_value) columnb
      from  sample_data,
            table(
                  cast(
                       multiset(
                                select  level
                                  from  dual
                                  connect by level <= length(regexp_replace(columnb || '|','[^|]'))
                       as sys.OdciNumberList
      order by columna,
               column_value
       COLUMNA COLUMNB
           123 abc
           123 cde
           123 fgr
           345 def
           345 ght
           345 sew
           890 deq
           890 nmk
           890 lop
           890 lip
           890 fre
       COLUMNA COLUMNB
           890 dws
    12 rows selected.
    SQL> SY.

  • Problem in displaying the data of columns into rows in sap script

    hi,
    i am working on a sap script and i have to display the dat which is displayed in column into rows but it is not displaying it properly.
    eg, C
        12.1
        Si
        5.5
    it is displaying the data right now like this but i want to display the  data like this:-
    eg, C      Si
        12.1   5.5
    plzzprovide me guidelines how to solve this problem.

    hi,
    i am using this code to display the data:-
    plzz provide me guidelines where i am getting wrong?
    TOPparCOMPONENT DESP,,,,,, INS. LOT #, , , , , , MIC,,,,,,,,,, MIC VALUEparENDTOPparFINAL
    PROTECT
    IF &I_FINAL-PRUEFLOS& NE '000000000000'
    &I_FINAL-MAKTX(23)&&i_final-prueflos(12Z)&
    &I_FINAL-kurztext(25)&
    &I_FINAL-original_input(8)&
    ELSE
    &I_FINAL-MAKTX(23)&     
    &I_FINAL-kurztext(25)&
    &I_FINAL-original_input(8)&
    ENDIF
    ENDPROTECT
    ITEMHEAD
    POSITION WINDOW
    SIZE WIDTH +0 . 4 CH HEIGHT +1 LN
    BOX FRAME 10 TW
    BOX HEIGHT '1.35' LN INTENSITY 20
    IF &PAGE& = '1'
    BOX XPOS '0' CH YPOS '0' CM WIDTH '0' CM HEIGHT '43' LN FRAME '10' TW
    For horizontal line at top
    BOX XPOS '0' CH YPOS '0' CM WIDTH '75' CH HEIGHT '0' LN FRAME '10' TW
    COLUMN LINES...
    END OF COLUMN LINES...
    BOX XPOS '0' CH YPOS '43' LN WIDTH '75' CH HEIGHT '0' LN FRAME '10'TW
    BOX XPOS '75' CH YPOS '0' LN WIDTH '0' CH HEIGHT '43' LN FRAME '10'TW
    ELSE
    COLUMN LINES...
    END OF COLUMN LINES...
    BOX XPOS '0' CH YPOS '0' CM WIDTH '0' CM HEIGHT '47' LN FRAME '10' TW
    BOX XPOS '0' CH YPOS '0' CM WIDTH '75' CH HEIGHT '0' LN FRAME '10' TW
    BOX XPOS '0' CH YPOS '0' CM WIDTH '45' CM HEIGHT '0' LN FRAME '10' TW
    BOX XPOS '20' CH YPOS '0' CM WIDTH '0' CM HEIGHT '47' LN FRAME '10' TW
    BOX XPOS '0' CH YPOS '47' LN WIDTH '75' CH HEIGHT '0' LN FRAME '10'TW
    BOX XPOS '75' CH YPOS '0' LN WIDTH '0' CH HEIGHT '47' LN FRAME '10'TW
    ENDIF
    LINEFEED
    NEWPAGE
    NEW-PAGE
    provide me guidelines to solve this problem.
    Edited by: ricx .s on Mar 13, 2009 5:58 AM

  • Transpose columns into rows

    hi ,
    i need to transpose columns into rows ,
    i know i can use the UNION ALL but my num of columns will most likely not be fixed so how can i do that ?
    pls advise

    This is from one of the forms link,, i reallyy dont know the link, but i guess this is "adrains" code
    SQL> WITH ilv AS (
      2      SELECT str || ','                                   AS str
      3      ,     (LENGTH(str) - LENGTH(REPLACE(str, ','))) + 1 AS no_of_elements
      4      FROM   t
      5      )
      6  SELECT RTRIM(str, ',')                              AS original_string
      7  ,      SUBSTR(str, start_pos, (next_pos-start_pos)) AS single_element
      8  ,      element_no
      9  FROM  (
    10         SELECT ilv.str
    11         ,      nt.column_value AS element_no
    12         ,      INSTR(
    13                   ilv.str,
    14                   ',',
    15                   DECODE(nt.column_value, 1, 0, 1),
    16                   DECODE(nt.column_value, 1, 1, nt.column_value-1)) + 1 AS start_pos
    17         ,      INSTR(
    18                   ilv.str,
    19                   ',',
    20                   1,
    21                   DECODE(nt.column_value, 1, 1, nt.column_value)) AS next_pos
    22         FROM   ilv
    23         ,      TABLE(
    24                   CAST(
    25                      MULTISET(
    26                         SELECT ROWNUM FROM dual CONNECT BY ROWNUM < ilv.no_of_elements
    27                         ) AS number_ntt )) nt
    28        );
    ORIGINAL_STRING                 SINGLE_ELEMENT  ELEMENT_NO
    X,Y,Z                           X                        1
    X,Y,Z                           Y                        2
    X,Y,Z                           Z                        3
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  XXX                      1
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  Y                        2
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  ZZ                       3
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  AAAAA                    4
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  B                        5
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  CCC                      6
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  D                        7
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  E                        8
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  F                        9
    XXX,Y,ZZ,AAAAA,B,CCC,D,E,F,GGG  GGG                     10
    13 rows selected.
    Note that the above SQL performs the following steps:
        * determines how many elements are in each string (WITH clause);
        * for each string, generates a collection of n elements (TABLE expression), where n is the derived number of elements in the string. Note in particular the use of "less than" in the "CONNECT BY ROWNUM < ilv.no_of_elements" on line 26. In all versions other than 10.1.x, this will need to be "CONNECT BY ROWNUM <= ilv.no_of_elements" (i.e. "less than or equal to"). There is an unusual bug with this row-generation technique in 10.1 that generates an additional row from the CONNECT BY;
        * uses the generated rows in a Cartesian Product with the original data to generate n rows per string, based on the above definition of n;
        * calculates the start and end position of each element in each string (INSTR); and
        * cuts each element from each string (SUBSTR).

  • How to split image into smaller (same size) pieces?

    Hi all,
    My question is how to split image into smaller (same size) pieces, using Photoshop elements 13? Could anyone help me with this one?
    Thanks!

    Use the Expert tab in Editor (I think that is what it is called in PSEv.13)
    You may find the grid helpful. Go to View>Grid. It will not print, but will help to orient you. You can set up the gridlines to suit via Edit>Preferences>Guides and Grid. If you want to partition the picture in to 4 uniform pieces, it would be Gridline every 50%, Subdivision 1. Also, go to View>Snap to>Grid.
    Set up the Rectangular marquee tool: If the picture is 6" wide & 4" high, enter width=3in & height=2in.on the tool's option bar. This will be a fixed size.
    Click and select one quadrant, press CTRL+J to place this quadrant on a separate layer
    Repeat for the other 3 quadrants
    You should end up with 5 layers : Background, and layers 1, 2, 3, 4.

  • To convert columns into row

    Hi All,
    I need help in building view which actually can show columns data as row.
    e.g.
    row is as follows
    Name Age Salary
    ABC 25 10000
    BBC 28 12000
    The above tables data I want to get as
    Name ABC BBC
    Age 25 28
    Salary 10000 12000
    Thanks in advance.

    Even if I don't really understand such requirement, I wrote some times ago such function to play around that :
    Re: Converting Columns into rows
    Nicolas.

  • Converting columns into rows

    Dear all....I need to convert all columns into rows in a table. For example table has following columns:
    Emp_Cod........Val1......Val2......Val3
    1 a b c
    Now I wish that each column should display as a value like:
    Emp_Cod........Val1
    1 a
    1 b
    1 c
    Now the one way to solve this job is to write a union statement for each column but for this I'll have to write equal number of select statements as there are columns.
    What I need that is there anyway to write minimum code for this job, is there any alternate way???

    SQL> with t as(select 1 emp_code, 'a' val1, 'b' val2, 'c' val3 from dual)
      2  select*from t unpivot(v for c in(val1,val2,val3));
    EMP_CODE  C     V                                                      
            1  VAL1  a                                                      
            1  VAL2  b                                                      
            1  VAL3  c                                                      
    SQL> col COLUMN_VALUE for a20
    SQL> with t as(select 1 emp_code, 'a' val1, 'b' val2, 'c' val3 from dual)
      2  select*from t,table(sys.odcivarchar2list(val1,val2,val3));
    EMP_CODE  V  V  V  COLUMN_VALUE                                        
            1  a  b  c  a                                                   
            1  a  b  c  b                                                   
            1  a  b  c  c                                                   

  • How to freeze column and row headers of a table

    How to freeze column and row headers of a table in jsp and javascript. An example is available in
    http://www.massless.org/_tests/grid1/ pls help to find a solutionj
    Thanks in anticipation
    Sreejesh

    At least I don't stop you from that. I also don't see any benefits in this topic.
    Success.

  • How to split strings into columns

    Hi guys,
    I have a select like this:
    select 'testing1'||'.'||'testing2'||'.'||'testing3' from dualwhich gives me an output like this:
    testing1.testing2.testing3How can I split this into 3 columns in SQL PLUS?
    Thank you in advance

    user643734 wrote:
    Thank you guys for all your help.
    Hi,
    I tried to resolve your problem using somme functions...
    I know is a long, long story but it works.
    Regards,
    Ion--create table_pivot
    CREATE TABLE pivot_table(id VARCHAR2(1) primary key, all_concat VARCHAR2(1810));
    --add data
    insert into pivot_table (id, all_concat) values('x','12345.2829303132.234234.234234.234234');
    insert into pivot_table (id, all_concat) values('y','67890.2324252627.234234.234234.234234.332545');
    insert into pivot_table (id, all_concat) values('z','11121314.12345.234234.234234.234234.23432.32453245.345435.345435');
    insert into pivot_table (id, all_concat) values('a','151617.67890.234234.234234');
    insert into pivot_table (id, all_concat) values('b','1819202122.1112131415.234234.234234.234234.345435');
    insert into pivot_table (id, all_concat) values('c','2324252627');
    insert into pivot_table (id, all_concat) values('h','2829303132.234234.234234.234234.23432.32453245.345435.345435.4325435.345');
    insert into pivot_table (id, all_concat) values('r','');
    set termout off
    --procedures and functions to compile
    --1. Main function to format strings
    create or replace function fgetformatstring
    (v_max_rows numeric, v_crt_row varchar2, v_table_source varchar2, v_save_to_table varchar2, v_option numeric)
    return varchar2
    is
    v_crt_char      numeric:=0;
    v_pos_char      numeric:=1;
    v_count      numeric:=0;
    v_string_out varchar2(500);
    v_str_paded     varchar2(10):=',null';
    v_length_pad     numeric;
    v_diff_pad numeric;
    l_crt_row varchar2(1800);
    v_char char:=',';
    v_delimiter char:='.';
    l_save_to_table varchar2(1000);
    l_max_rows     numeric;
    begin
    l_crt_row:=replace(v_crt_row,'.' ,',');
    l_save_to_table:=v_save_to_table;
    l_max_rows:=v_max_rows;
    if v_option=0 then     --get the ',null' to paded in pivot_table     
    loop
              v_pos_char:=v_crt_char;
              v_crt_char:=instr(v_crt_row,v_char, v_crt_char+1);
              v_count:=v_count+1;
    exit when v_crt_char=0 or v_count=1000;
         end loop;
         v_string_out:=' ';
         v_count:=v_count-1;
         v_diff_pad :=l_max_rows-1-v_count;
    if v_diff_pad >0 then                    --nothing to add
         v_length_pad:=length(v_str_paded);-- (+v_char)
         v_length_pad:=v_diff_pad*v_length_pad;
         v_length_pad:=v_length_pad+1;
         v_string_out:=rpad(v_string_out,v_length_pad,v_str_paded);
    end if;
    end if;
    if v_option=1 then     --get definition of v_save_to_table
         v_count:=1;
         v_string_out:=' ';
         loop
    exit when v_count=l_max_rows+1 or v_count>=1000;
    v_length_pad:=length(v_string_out||'col'||to_char(v_count)||',');
              v_string_out:=rpad(v_string_out,v_length_pad,'col'||to_char(v_count)||',');
              v_count:=v_count+1;
         end loop;
    v_string_out:=trim(v_char from v_string_out);
              --v_string_out:='id,'||v_string_out;
              v_string_out:=v_string_out;
    end if;
    if v_option=2 then     --get position of last comma  
    loop
              v_pos_char:=v_crt_char;
              v_crt_char:=instr(v_crt_row,v_char, v_crt_char+1);
              v_count:=v_count+1;
    exit when v_crt_char=0 or v_count=1000;
         end loop;
         v_string_out:=substr(v_crt_row,1,v_pos_char-1);
    end if;
    if v_option=3 then     --get numbers of delimiters(.)
    loop
              v_pos_char:=v_crt_char;
              v_crt_char:=instr(v_crt_row,v_delimiter, v_crt_char+1);
    --dbms_output.put_line( 'Rows: ' ||v_crt_row|| ' iteration: '||v_count);
              v_count:=v_count+1;
    exit when v_crt_char=0 or v_count=1000;
         end loop;
         v_count:=v_count;
         v_string_out:=to_char(v_count);
    end if;
    if v_option=4 then     --get sql command to create v_save_to_table
         v_count:=1;
         v_string_out:=' ';
         loop
    exit when v_count=l_max_rows+1 or v_count>=1000;
    v_length_pad:=length(v_string_out||'col'||to_char(v_count)||' varchar2(40), ');
              v_string_out:=rpad(v_string_out,v_length_pad,'col'||to_char(v_count)||' varchar2(40), ');
              v_count:=v_count+1;
         end loop;
    v_string_out:=trim(' ' from v_string_out);
              v_string_out:=trim(v_char from v_string_out);
              v_string_out:='create table '|| l_save_to_table ||'('||v_string_out||')';
    end if;
    if v_option=5 then     --get numbers of delimiters(,) 
    loop
              v_pos_char:=v_crt_char;
              v_crt_char:=instr(v_crt_row,',', v_crt_char+1);
              v_count:=v_count+1;
    exit when v_crt_char=0 or v_count=1000;
         end loop;
         v_count:=v_count-1;
         v_string_out:=to_char(v_count);
    end if;
    dbms_output.put_line( 'string: ' ||v_string_out|| ' option: '||v_option);
    return v_string_out;
    end;
    --2.get max items
    create or replace function fgetmaxitems
    --get the max number of items founded in the pivot_table.all_concat
    --before change the points with comma
    return numeric
    is
    v_max                numeric:=0;
    v_field               pivot_table.all_concat%type;
    v_crt_max numeric:=0;
    v_crt_row     varchar(1000);
    cursor c1 is
    select all_concat from pivot_table;
    begin
    v_crt_max:=0;
    v_max:=0;
    open c1;
    loop
    fetch c1 into v_field;
    exit when c1%notfound;
    v_crt_row:=v_field;
    v_crt_max:=fgetformatstring(0,v_crt_row,'pivot_table','',3);
    if v_crt_max>v_max then
         v_max:=v_crt_max;
    end if;
    end loop;
    close c1;
    return v_max;
    end;
    --3. insert the rows in table_dest
    create or replace procedure pinsertrow
    v_max_rows numeric,
    v_all_concat varchar2,
    v_source_table_name varchar2,
    v_save_to_tablename varchar2
    is
    v_sql_string      varchar2(1000);
    l_all_concat     varchar2(1500);
    begin
    l_all_concat:=replace(v_all_concat,'.' ,',');
    v_sql_string:='('||l_all_concat||')';
    v_sql_string:=' insert into ' ||v_save_to_tablename
    ||' (' ||fgetformatstring(v_max_rows,l_all_concat,v_source_table_name,v_save_to_tablename,1)||')'||
    ' values'||v_sql_string ;
    execute immediate v_sql_string;
    end;
    --4.procedure to create dinamically <table_dest>
    create or replace procedure pcreatesavetable
    (v_table_source varchar2, v_save_to_table varchar2)
    is
    v_sql_string      varchar2(1000);
    already_exists exception;
    pragma exception_init(already_exists,-955);
    v_max numeric;
    begin
    --clear empty values;
    v_sql_string:='delete from ' ||v_table_source|| ' where length(all_concat)=0 or (all_concat) is null';
    execute immediate v_sql_string;
    v_sql_string:='';
    v_max:=fgetmaxitems;
         --create new table
         v_sql_string:=fgetformatstring(v_max,'all_concat', v_table_source,v_save_to_table,4);
    dbms_output.put_line( 'sql create : '|| v_sql_string);
    execute immediate v_sql_string;
    exception
         when already_exists then
    --delete old table
         v_sql_string:='drop table ' ||v_save_to_table;
         execute immediate v_sql_string;
         --create new table
         v_sql_string:=fgetformatstring(v_max,'all_concat', v_table_source,v_save_to_table,4);
    dbms_output.put_line( 'sql recreate final_table : '|| v_sql_string);
    execute immediate v_sql_string;
    end;
    --5.main procedure
    create or replace procedure pmainproc(v_save_to_table varchar2)
    is
    v_key_pivot      pivot_table.id%type;
    v_column_pivot     pivot_table.all_concat%type;
    v_max numeric;
    v_crt_row          varchar2(1600);
    cursor c2 is select y.id , y.all_concat
         from pivot_table y
    ;     --cursor upon pivot_table 
    begin
    v_max:=fgetmaxitems;
    if v_max <> 0 then
    --clear empty values;
    delete from pivot_table where length(all_concat)=0 or (all_concat) is null;
    --change all points with comma
    update
    (select x.id, x.all_concat from pivot_table x, pivot_table y where x.id=y.id) b
    set b.all_concat=replace(b.all_concat,'.', ',');
    -- padd the all_concat with ',null'
    update
    (select x.id, x.all_concat from pivot_table x, pivot_table y where x.id=y.id and
    fgetformatstring(v_max,x.all_concat,'pivot_table',v_save_to_table,5) <> v_max
    ) b
    set b.all_concat=b.all_concat || fgetformatstring(v_max,b.all_concat,'pivot_table',v_save_to_table,0);
    --remove last
    update pivot_table
    set all_concat= fgetformatstring(v_max,all_concat,'pivot_table',v_save_to_table,2)
    where fgetformatstring(v_max,all_concat,'pivot_table',v_save_to_table,5) = v_max;
    open c2;
    loop
    fetch c2 into      v_key_pivot, v_column_pivot;
    exit when c2%notfound;
    v_crt_row:=v_column_pivot;
    -- insert data in to final_table
    pinsertrow(v_max,v_crt_row,'pivot_table',v_save_to_table);
    end loop;
    close c2;
    end if;
    end;
    --create table_dest
    --the table_dest will be created automatically, so you need admin privilege to run that before compile pcreatesavetable :
    connect / as sysdba or connect system/password
    --grant create table to hr;
    --conne hr/hr;
    exec pcreatesavetable('pivot_table','table_dest');
    --add data to table_dest
    exec pmainproc('table_dest');
    set termout on
    select * from table_dest;
    Table created.
    1 row created.
    1 row created.
    1 row created.
    1 row created.
    1 row created.
    1 row created.
    1 row created.
    1 row created.
    Function created.
    Function created.
    Procedure created.
    Procedure created.
    Procedure created.
    PL/SQL procedure successfully completed.
    PL/SQL procedure successfully completed.
    COL1 COL2 COL3 COL4 COL5 COL6 COL7 COL8 COL9 COL10
    12345 2829303132 234234 234234 234234
    67890 2324252627 234234 234234 234234 332545
    11121314 12345 234234 234234 234234 23432 32453245 345435 345435
    151617 67890 234234 234234
    1819202122 1112131415 234234 234234 234234 345435
    2324252627
    2829303132 234234 234234 234234 23432 32453245 345435 345435 4325435 345
    7 rows selected.

  • Split column into multiple text and number columns

    I'm trying to figure out how to split this column into multiple columns with power query. One column for the company name/person name, one for the address, one for the zip.  Some of the addresses have a three or four digit code before the address, which
    I would like in its own column too.  It's the 170 on the lastname, firstname line.  Does anyone have any pointers on this? I'm familiar with PQ advanced editor, but struggling with this one.  
    COMPANY INC. 195 MAIN ST MYCITY ST 12345
    LASTNAME, FIRSTNAME 170 477 ANY STREET CIRCLE  MYCITY ST 12345
    Thanks for your help!

    HI Gil,
    We have column with more than one numbers separated by space or comma or semicolon.
    We need to add the row for each number by keeping all other column value same.
    Here is a original table
    Col1
    Col2
    Col3
    A
    B
    11 22,33 44; 55
    C
    D
    10    20
    and expected output should be
    Col1
    Col2
    Col3
    A
    B
    11
    A
    B
    22
    A
    B
    33
    A
    B
    44
    A
    B
    55
    C
    D
    10
    C
    D
    20
    Please let us know the best way to solve this...

  • Convert single column into rows

    hi Gurus,
    I have one table test colums are id and name.
    id number
    name varchar2
    data is like
    id name
    1 xy
    2 xyy
    3 mm
    4 pp
    Now my requirement is to convert single column id into rows
    i,e my output should be of singel rows like :- 1,2,3,4
    How to achive this result .
    I dont have any idea to do this query.
    Please help guys.
    Thanks in advance.
    Vijay

    Well,
    As long as your code doesn't have to run in production, simplest way is:
    WM_CONCAT (but it's not documented)
    or use XMLAGG, it's simpler than a connect by:
    MHO%xe> with t as (
      2  select 1 col, 'xy' str from dual union all
      3  select 2, 'xyy' from dual union all
      4  select 3, 'mm'from dual union all
      5  select 4, 'pp' from dual union all
      6  select 8, 'pp' from dual union all
      7  select 12, 'pp' from dual union all
      8  select 40, 'pp' from dual
      9  )-- actual query, based on id's generated above:
    10  select rtrim(xmlagg(xmlelement(e,col||',')).extract('//text()'),',') col
    11  from   t;
    COL
    1,2,3,4,8,12,40
    1 rij is geselecteerd.

Maybe you are looking for

  • How can i transfer my current itunes library to my new 3GS?

    Hey; i would be grateful if anyone could help me transfer my current itunes library synced with my 1st gen Touch to my new (ordered) 16gb 3GS? this probably seems like a stupid question but i am hardly an itunes expert! thank-you...

  • IMAGE IN A FORM

    I'm trying to embed an image in a form. For example: show a persons picture with his name address etc. Can someone tell me exactly how to do this? The form will contain fields that are updateable. I tried this: Created a region; Created items A,B,C i

  • How do I attach a video in my email?

    How do I attach a video in my email?

  • Photo Book: Blur images in A3 and in a PDF

    Hi everyone, I'm making a photobook in A3, and I have a problem in it. My pictures are in high-resolution. When I import them in my photobook, they become very blur, and it's the same things when I export my book as PDF in order to have a preview. I

  • Can't update to 10.6.2 on Snow Leopard

    Hi, I'm currently running 10.6.0. As I wanted to install the latest iPhone SDK 4 I got the error that I have to update to 10.6.2 before. So I run the software update application, but it says that I'm already using the latest software.