divide the string separated by commas into columns

Hello
I have the following string
str := 'abcd,123,defoifcd,87765'
The above string must be divided into 4 different columns

How can I achieve that

Thank you

Hello

Use REGEXP_SUBSTR:

SELECT  REGEXP_SUBSTR (str, '[^,]+', 1, 1)    AS part_1
,       REGEXP_SUBSTR (str, '[^,]+', 1, 2)    AS part_2
,       REGEXP_SUBSTR (str, '[^,]+', 1, 3)    AS part_3
,       REGEXP_SUBSTR (str, '[^,]+', 1, 4)    AS part_4
FROM    table_x
;

Str can contain foul? For example, you can have a string like ' foo, bar ", where you want to count part_2 and part_3 as NULL and 'bar' is part_4? If so:

SELECT  RTRIM (REGEXP_SUBSTR (str, '[^,]*,', 1, 1), ',')    AS part_1
,       RTRIM (REGEXP_SUBSTR (str, '[^,]*,', 1, 2), ',')    AS part_2
,       RTRIM (REGEXP_SUBSTR (str, '[^,]*,', 1, 3), ',')    AS part_3
,       LTRIM (REGEXP_SUBSTR (str, ',[^,]*', 1, 3), ',')    AS part_4
FROM    table_x
;

Published by: Frank Kulash, February 14, 2012 08:46

Tags: Database

Similar Questions

  • Convert the string separated by commas in the rows

    Dear gurus,

    I want to convert the string separated by commas in the rows to insert in the collection.

    for example, the string 1234,2323,23232,2343,34234
    Higher up in the chain must be converted to ranks in order to insert into the table or the collection

    Thanks in advance
    Sanjeev

    String in rows separated to convert the comma.

    with t
    as
    (
    select '1234,2323,23232,2343,34234' as txt from dual
    )
    select REGEXP_SUBSTR (txt, '[^,]+', 1, level)
      from t
     connect by level <= length(regexp_replace(txt,'[^,]*'))+1
    
  • Build the string separated by commas in query with Expression box sub

    Oracle 10g on Windows XP

    Consider the following query:

    Select batch_id, tr state_tag_seq where request_type in (select case WHEN PX = 'RETURN' AND 'TRANSFER' |) «, » || "PUBLIC TRANSIT"
    WHEN PX = "RFT" THEN "TRANSIT".
    ELSE 'Invalid '.
    END
    OF THE DOUBLE
    ) ;


    In this case, PX is accepted to execution of the user interface.

    I'm having no rows returned (all valid lines matching in DB) for the following case assessment

    WHEN PX = 'RETURN' THEN 'TRANSFER '. «, » ||' Transit "(PX when is evaluated to 'RETURN')

    The application works well when it px IS estimated to be "PCR" (as the evaluated expression is just a string ('TRANSIT') single, not separated by commas, as in the other case)

    Hiow do rewrite this query, if it gives a correct result? In other words, I actually build the query string slot, which is assessed by the external when query clause.

    I think it might be a specific way to use quotes in my case expression, but I'm reaching an impasse. Any help is appreciated.

    Thank you
    SR

    You can try

    select batch_id, state_tag_seq
      from tr
     where request_type member of case
                             when px = 'RETURN'
                             then
                              sys.dbms_debug_vc2coll (
                               'TRANSFER',
                               'TRANSIT'
                              )
                             when px = 'RFT'
                             then
                              sys.dbms_debug_vc2coll ('TRANSIT')
                             else
                              sys.dbms_debug_vc2coll ('Invalid')
                             end
    
  • String separated by commas as input and display output

    Hello

    I have a requirement that I have to take a string separated by commas as entry (may be in a table?) and display the output.

    For example, executions should be like this:

    Please enter the string separated by commas:

    A, B, C, D (entries of the user)

    Output must be

    You entered:
    A
    B
    C
    D


    How can do us in PL/SQL?

    Thank you
    Pramod
    /* Formatted on 2012/06/12 09:22 (Formatter Plus v4.8.8) */
    SELECT     REGEXP_SUBSTR ('A,B,C,D', '[^,]+', 1, LEVEL) AS re
          FROM DUAL
    CONNECT BY LEVEL <= NVL (LENGTH (REGEXP_REPLACE ('A,B,C,D', '[^,]', NULL)), 0) + 1;
    
  • How to split string separated by commas and pass to the clause of the select statement

    Referring to article How to divide string separated by commas, then pass to clause of a select statement, tquery that there the following plan:

    Query1:

    select * from emp where ename in (
        select regexp_substr('SMITH,ALLEN,WARD,JONES','[^,]+', 1, level) from dual
        connect by regexp_substr('SMITH,ALLEN,WARD,JONES', '[^,]+', 1, level) is not null );
    

    Base1:

    Plan hash value: 4242290184
    
    
    --------------------------------------------------------------------------------------------
    | Id  | Operation                       | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
    --------------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT                |          |     1 |   133 |     7  (29)| 00:00:01 |
    |*  1 |  HASH JOIN                      |          |     1 |   133 |     7  (29)| 00:00:01 |
    |   2 |   VIEW                          | VW_NSO_1 |     1 |    46 |     3  (34)| 00:00:01 |
    |   3 |    HASH UNIQUE                  |          |     1 |       |     3  (34)| 00:00:01 |
    |*  4 |     CONNECT BY WITHOUT FILTERING|          |       |       |            |          |
    |   5 |      FAST DUAL                  |          |     1 |       |     2   (0)| 00:00:01 |
    |   6 |   TABLE ACCESS FULL             | EMP      |    14 |  1218 |     3   (0)| 00:00:01 |
    --------------------------------------------------------------------------------------------
    
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
    
       1 - access("ENAME"="$nso_col_1")
       4 - filter( REGEXP_SUBSTR ('SMITH,ALLEN,WARD,JONES','[^,]+',1,LEVEL) IS NOT NULL)
    

    However, the following query generates the plan I want:

    Query2:

    select * from emp where ename in ('SMITH','ALLEN','WARD','JONES');
    

    Plan2:

    Plan hash value: 3956160932
    
    
    --------------------------------------------------------------------------
    | Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
    --------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |      |     4 |   348 |     3   (0)| 00:00:01 |
    |*  1 |  TABLE ACCESS FULL| EMP  |     4 |   348 |     3   (0)| 00:00:01 |
    --------------------------------------------------------------------------
    
    
    Predicate Information (identified by operation id):
    ---------------------------------------------------
    
    
       1 - filter("ENAME"='ALLEN' OR "ENAME"='JONES' OR "ENAME"='SMITH' OR
                  "ENAME"='WARD')
    

    Can I change the query1 query for plan2?

    As Juliet was mentioned in the first SQL that you generate from the ENAME list that you must pass in the IN clause when executing. But in the second SQL, it passed as a static value. So first SQL must do more work. So you see a different execution plan.

    But this is a work around to get what you are looking for. But I can't say it's a foolproof method. But anyway here you go.

    SQL > var ename_list varchar2 (100)
    SQL > exec: ename_list: = 'SMITH, ALLEN, WARD, JONES ';

    PL/SQL procedure successfully completed.

    SQL > select *.
    2 of PEM
    where the 3 «,» | : ename_list | ',' like '%', | Ename | ',%';

    EMPNO, ENAME, JOB HIREDATE DEPTNO ID COM SAL MGR
    ---------- ------ --------- ---------- --------- ---------- ---------- ---------- ----------
    7369 SMITH COMMITTED 7902 2975 2 APRIL 81 0 20
    7499 ALLEN 7698 1600 20 FEBRUARY SALESMAN 81 300 30
    7521 WARD 7698 1250 22 FEBRUARY SALESMAN 81 500 30
    7566 JONES MANAGER 7839 2975 2 APRIL 81 0 20

    SQL > select * from table (dbms_xplan.display_cursor);

    PLAN_TABLE_OUTPUT
    -------------------------------------------------------------------------------------------------------
    SQL_ID, 848zhvbvgf7d6, number of children 0
    -------------------------------------
    Select * from emp where «,» | : ename_list | ',' like '%', | Ename
    || ',%'

    Hash value of plan: 2872589290

    --------------------------------------------------------------------------
    | ID | Operation | Name | Lines | Bytes | Cost (% CPU). Time |
    --------------------------------------------------------------------------
    |   0 | SELECT STATEMENT |      |       |       |     2 (100) |          |
    |*  1 |  TABLE ACCESS FULL | EMP |     1.    38.     2 (0) | 00:00:01 |
    --------------------------------------------------------------------------

    Information of predicates (identified by the operation identity card):
    ---------------------------------------------------

    1 - filter(','||:ENAME_LIST||',' LIKE '%,'||") ENAME "|", %')

    19 selected lines.

    SQL >

  • How to get the string (specified by row and column) of txt file with labview

    Hello world

    How to get the string (specified by row and column) of txt file with labview

    THX

    As far as I know, a text file has no column.  Be more specific.  Do you mean something like the 5th word on line 4, where the words are separated by a space, and lines are separated by a newline character?  You can read from the spreadsheet String function and set the delimiter to a space.  This will produce a 2D channels table.  Then use the table to index and give the line number and column number.

  • How to store the values separated by commas

    Hi all

    I have a table named discount, with discount_id (number data type) as one of the columns.
    The user gives an entry as the value separated by commas, (ex: '123,27890,3543')
    I use built-in proc that separates the values separated by commas.
    DECLARE
       l_input   VARCHAR2 (4000) := '123,27890,3543';
       l_count   BINARY_INTEGER;
       l_array   DBMS_UTILITY.lname_array;
    BEGIN
       DBMS_UTILITY.comma_to_table (
          list     => REGEXP_REPLACE (l_input, '(^|,)', '\1x'),
          tablen   => l_count,
          tab      => l_array);
       DBMS_OUTPUT.put_line (l_count);   
    
       FOR i IN 1 .. l_count
       LOOP
          DBMS_OUTPUT.put_line (
                'Element '
             || TO_CHAR (i)
             || ' of array contains: '
             || SUBSTR (l_array (i), 2));
       END LOOP;
    END;
    
    Result:
    3
    Element 1 of array contains: 123
    Element 2 of array contains: 27890
    Element 3 of array contains: 3543
    Result set is stored in a table.
    I would like to do a select on the table of discounts
    Select * discount where discount_id (123, 27890 3543).

    I am looking for options,
    I need to create a new physical table as
    Create table new_table (identification number) and bulk insert in this table.
    Select * discount where discount_id in (select distinct id of new_table).
    or
    Is there a better way?

    THX
    Rod.

    Hello, SamFisher.
    May be the recursive subquery factoring help you?

    WITH test(f1, n) AS (select '12, 15, 235', 1 from dual
                         UNION all
                         select f1, n + 1 from test where n < regexp_count('12, 15, 235', ',') + 1)
    SELECT regexp_substr(f1, '[^, ]+', 1, n) f
    FROM test
    
  • How to get the values separated by commas of multiple records in table

    How to get the values separated by commas of multiple records in table

    for example

    name address age sex
    a 12 m e
    b hh 12 f
    BB c 13 h

    I need to get output as a, b, c from a query

    Use the query as below he works for me, change the names of tables and columns

    SELECT SUBSTR (SYS_CONNECT_BY_PATH (PROXY_EMAIL, ','), 2) csv FROM (SELECT PROXY_EMAIL, ROW_NUMBER () ON the rn (ORDER OF PROXY_EMAIL), COUNT (*) NTC (STARS_PROXY_ASSIGNMENT) WHERE EMPLID = #EMPLID) WHERE rn = cnt START WITH rn = 1 CONNECT BY rn = rn + 1 ADVANCE

  • I'm trying to extract the strings and load them into a text-helpplease file

    I'm trying to extract the strings and load them into a file text without space between them - see below

    p_csa = ISCC M4 DP

    where p_csa is a tring to load into a text file



    Code:

    SELECT SUBSTR (p_csa, 0, 0) INTO v_telco FROM DUAL;
    SELECT SUBSTR (p_csa, 1, 4) INTO v_comp FROM DUAL;
    SELECT SUBSTR (p_csa, 5.3) INTO v_csa_prefix FROM DUAL;
    SELECT SUBSTR (p_csa, 8, 4) INTO v_csa_type FROM DUAL;

    fileidinvoice: = UTL_FILE. FOPEN (c_data_location, filenameinvoice, 'W');
    fileidlog: = UTL_FILE. FOPEN (c_data_location, filenamelog, 'W');

    UTL_FILE. PUT_LINE)
    fileidinvoice, v_telco | » '|| v_comp | "|" | v_csa_prefix: "| v_csa_type);


    -the content of the file-

    ISCC M4 DP

    =====================
    I want to remove the space between the chain above for the content of the file should look like this:

    -the content of the file-

    GCSIM4DP

    Hello

    976253 wrote:
    I'm trying to extract the strings and load them into a file text without space between them - see below

    p_csa = ISCC M4 DP

    where p_csa is a tring to load into a text file

    Code:

    SELECT SUBSTR (p_csa, 0, 0) INTO v_telco FROM DUAL;

    Is it always NULL?

    SELECT SUBSTR (p_csa, 1, 4) INTO v_comp FROM DUAL;

    You don't need the double table much in PL/SQL. You can simply say

    v_comp := SUBSTR (p_csa, 1, 4);
    

    Almost all integrated single-row functions work in PL/SQL. (NVL2 is the only one I know who does not work in PL/SQL).

    SELECT SUBSTR (p_csa, 5.3) INTO v_csa_prefix FROM DUAL;
    SELECT SUBSTR (p_csa, 8, 4) INTO v_csa_type FROM DUAL;

    fileidinvoice: = UTL_FILE. FOPEN (c_data_location, filenameinvoice, 'W');
    fileidlog: = UTL_FILE. FOPEN (c_data_location, filenamelog, 'W');

    UTL_FILE. PUT_LINE)
    fileidinvoice, v_telco | » '|| v_comp | "|" | v_csa_prefix: "| v_csa_type);

    What are you trying to do here? For example, what you're trying to put between c_comp and v_csa_prefix?

    -the content of the file-

    ISCC M4 DP

    =====================
    I want to remove the space between the chain above for the content of the file should look like this:

    -the content of the file-

    GCSIM4DP

    If you don't want space, then only concatenate all the spaces in the string before you write it and REPLACE allows you to delete all the spaces that have been copied from p_csa. For example:

    UTL_FILE.PUT_LINE ( fileidinvoice
                , REPLACE ( v_telco || v_comp
                                        || v_csa_prefix
                              || v_csa_type
                       , ' '
                       )
                );
    

    I guess you need the v_comp of separate variables, the v_csa_prefix and the v_csa_type for something.

    I hope that answers your question.
    If this isn't the case, after a complete script that people can run to recreate the problem and test their ideas. Display the results you want (in other words, the contents of the file created).
    Always say what version of Oracle you are using (for example, 11.2.0.3.0).
    See the FAQ forum {message identifier: = 9360002}

  • Counting of string separated by commas in the cells of a column

    Hi all

    Try to pass some of my Excel spreadsheets in numbers. I'd appreciate help with a specific string of text/number in the cells in a column. In the example below (no sheet of complete works) is a column, the packs work (WP01, WP02 and so forth)-which means work packs a particular Test & Validation document (e.g. TV-102) applies to.

    I would like to be able to simple counting the number of occurrences, say "WP05", is applicable. If I use COUNTIF, on the "WorkPack" column, he has only the cells that contain only the string "WP05" (i.e. in the sheet, I work with, I know there are 22 entries for "WP05" in the column of pack work, but only the 14 account)

    Note: I am building this on my MAC, but would like to be able to access and use these sheets on my iPad - don't know if that makes a difference.

    Any suggestions / ideas?

    Thank you in advance...

    ATB Perry

    ID

    Paper group

    WorkPack

    TV-91

    Validation of NFR

    WP05 WP06

    TV-102

    Certificate of conformity

    WP05 WP06

    TV-103

    Validation of NFR

    WP05 WP06

    TV-206

    Test EN

    WP05

    TV-207

    Test EN

    WP05

    TV-208

    Test EN

    WP05

    TV-209

    Test EN

    WP05

    TV-212

    Certificate of conformity

    WP01, WP02 WP03, WP04, WP05, WP06, WP07, WP08 MICROSOFT WINDOWS CURRENTVERSION, WP09, WP10, WP11, WP12, WP13, WP14, WP.15

    TV-213

    Validation of NFR

    WP01, WP02 WP03, WP04, WP05, WP06, WP07, WP08 MICROSOFT WINDOWS CURRENTVERSION, WP09, WP10, WP11, WP12, WP13, WP14, WP.15

    TV-214

    Test EN

    WP01, WP02 WP03, WP04, WP05, WP06, WP07, WP08 MICROSOFT WINDOWS CURRENTVERSION, WP09, WP10, WP11, WP12, WP13, WP14, WP.15

    TV-215

    Validation of NFR

    WP01, WP02 WP03, WP04, WP05, WP06, WP07, WP08 MICROSOFT WINDOWS CURRENTVERSION, WP09, WP10

    TV-308

    Validation of NFR

    WP01, WP02 WP03, WP04, WP05, WP06, WP07, WP08 MICROSOFT WINDOWS CURRENTVERSION, WP09, WP10, WP11, WP12, WP13, WP14, WP.15

    TV-338

    Test EN

    WP05

    Here's how I would approach (without more):

    Add additional columns to your table (those titled "WP01',"WP02", etc.).  Enter the names as shown

    The first line is a heading row.

    Also... Add an extra line at the end and make a footer row

    D2 = (LEN ($C2) −LEN (REPLACE ($C2, D$ 1, ""))) ÷LEN ($1 D)

    It's shorthand dethrone select cell D2 and type e (or copy and paste it here) the formula:

    = (LEN ($C2) −LEN (REPLACE ($C2, D$ 1, ""))) ÷LEN ($1 D)

    Select cell D2, copy

    Select cells D2 the bottom of column R, paste

    in the footer row (row 15 of this example) iff the following formula:

    D15 = Sum (D)

    Select the cell D15, copy

    Select the cells D15 thru R15, dough

    I could never find 22 WP05 instances (even when I search your message on this web page):

  • How to recover the vales separated by commas in the same column in Oracle SQL

    Dear members

    Please give me the solution for the below question

    Oracle table

    Ename product
    A 1,2,3
    B 1,2,3


    Product column has data like this only separated by commas

    Output should be like this.

    Ename, product
    A 1
    A 2
    A 3
    B 1
    B 2
    B 3

    Can we get the output above using just Oracle SQL select statement?
    I tried in Google I got an answer in SQL server, there is something called "Cross apply split" function, I'm not sure in Oracle.

    Welcome to the forum.

    Here is another interesting site, with lots of useful examples:
    http://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:2189860818012 #2654179200346949517

    Also, be sure to read the SQL and PL/SQL FAQ:
    http://forums.Oracle.com/forums/Ann.jspa?annID=1535

  • Divide the string into rows according to the space

    I'm using Oracle 11.2.0.3.  I need a faster way to divide the organization names based on space as possible just using SQL.

    with org as

    (

    Select 1 org_pk, org_nm 'ALL american, INC.' of any double union

    Select org_pk 2, org_nm "COMPANY A.G" Union double all the

    Select org_pk 3, org_nm "GROWTH FUND SPONSORS and SONS, Inc." of the double

    )

    Select * org;

    Result, I need is

    1. ALL THE

    1 american

    1, INC.

    2 A.G

    2 COMPANY

    3. THE GROWTH

    3 FUND

    3 SPONSORS

    3 and

    3 WIRES,

    3 INC.

    Thank you very much.

    You can use GROUP BY with LISTAGG or XMLAGG. And ordinalite allows to preserve order:

    with org as)

    Select 1 org_pk, org_nm 'ALL american, INC.' of any double union

    Select 2 org_pk, 'A.G COMPANY' org_nm of all the double union

    Select org_pk 3, org_nm "GROWTH FUND SPONSORS and SONS, Inc." of the double

    )

    Select org_pk,

    x.org_nm,

    x.o

    org,.

    XMLTable)

    "ora: tokenize(.,"").

    by the way ' ' | org_nm

    columns

    path of varchar2 (4000) to org_nm '.'.

    o for the ordinalite

    ) x

    where x.org_nm is not null

    /

    ORG_PK ORG_NM O
    ---------- ------------------------------ ----------
    1 ALL                                     2
    1 American 3
    1 ,INC                                    5
    2 A.G                                     2
    2 COMPANY 4
    3 GROWTH                                  2
    3 FUND                                    3
    3 4 SPONSORS
    3 and                                     5
    3 SONS,                                   6
    3 INC.                                    7

    11 selected lines.

    SQL >

    However, I would like to use SUBSTR/INSTR plain + CONNECT BY if the volumes are large or performance is important.

    SY.

  • Simple question: how to divide the string into multiple lines concatenated?

    Hi people,

    Maybe it's an easy question.

    How to split a string that is concatenated into multiple lines by using the SQL query?

    ENTRY:
    select 'AAA,BBB,CC,DDDD' as data from dual
    Delimiter = ', '.

    Expected results:
    data
    ------------
    AAA
    BBB
    CCC
    DDDD
    I'm looking for something nice to feature "an opposite to «sys_connect_by_path»»

    Thank you
    Tomas
    with t as (select 'aaaa,,bbbb,cccc,dddd,eeee,ffff' as txt from dual)
    -- end of sample data
    select REGEXP_SUBSTR (txt, '[^,]+', 1, level)
    from t
    connect by level <= length(regexp_replace(txt,'[^,]*'))+1
    
    REGEXP_SUBSTR(TXT,'[^,]+',1,LE
    ------------------------------
    aaaa
    bbbb
    cccc
    dddd
    eeee
    ffff
    
  • string separated by commas of lines and columns

    Hello

    I have several channels, for example

    There are only numbers

    the length of the numbers is different as well as the length of the string to a number is always different

    Row1 111,112,113,114

    row2 22.23

    row3 1,2,3,4,5

    How can I turn in the following

    col1 col2

    111 112,113,114

    112 111,113,114

    113 111,112,114

    114 111,112,113

    22 23

    23 22

    1 2,3,4,5

    and so on...

    None righteous, I missed the fact that the OP asked for the numbers to be removed from the list...

    SQL > ed

    A written file afiedt.buf

    1 with t as (select ' 111,112,113,114' in the neck of double union all

    2 Select "22.23 ' in any union.

    3 select "1,2,3,4,5' double '.

    4            )

    5  --

    6 end of test data

    7  --

    8. Select level n

    9, trim ("," regexp_replace (col,'(.*?) (^|,)'|| regexp_substr (col, ' [^,] +', 1, level). » (,|$) (.*?) (($', '\1,\4')) in the neck

    regexp_substr 10, (col, ' [^,] +', 1, level) in the framework

    11 t

    12 connect by prior col = col

    13 and level<= regexp_count(col,="">

    14 * and prior sys_guid() is not null

    SQL > /.

    COL N PARTY

    ---------- --------------- ---------------

    1 2,3,4,5 1

    2 2 1,3,4,5

    3 3 1,2,4,5

    4 4 1,2,3,5

    5 5 1,2,3,4

    1 112,113,114 111

    2 111,113,114 112

    3 111,112,114 113

    4 111,112,113 114

    1 23 22

    2 22 23

    (regular expressions less performance best, wise)

    Please also note that you should not ask for your message to be marked as correct/good which is considered to be "begging for points" and is against the forum rules.

  • the analysis of string separated by commas

    Hello people,

    Indeed, this forum is great and has always helped me a lot. I hope that this time remains the same. Thank you guys for you continues a support and help.

    I have a value string as a-, b, c, d, e, f

    Just sql help, I want to put each value of the string above in a different line. If the output should be-

    one
    b
    c
    d
    e
    f

    using procedures would not only complex, but I just want to do by using queries.

    Please advise,
    Thank you.
    SQL> with t as (select 'ABC,DEF GHI,JKL' str from dual)
      2  select regexp_substr(str,'[^,]+', 1, level) list
      3  from t connect by level <= NVL( LENGTH( REGEXP_REPLACE( str, '[^,]+', NULL ) ), 0 ) + 1
      4  /
    
    LIST
    ---------------
    ABC
    DEF GHI
    JKL
    

Maybe you are looking for