Split function in oracle to comma separated values with automatic sequence

后端 未结 8 947
遇见更好的自我
遇见更好的自我 2020-11-28 09:37

Need Split function which will take two parameters, string to split and delimiter to split the string and return a table with columns Id and Data.And how to call Split funct

相关标签:
8条回答
  • 2020-11-28 10:42

    Best Query For comma separated in This Query we Convert Rows To Column ...

    SELECT listagg(BL_PRODUCT_DESC, ', ') within
       group(   order by BL_PRODUCT_DESC) PROD
      FROM GET_PRODUCT
    --  WHERE BL_PRODUCT_DESC LIKE ('%WASH%')
      WHERE Get_Product_Type_Id = 6000000000007
    
    0 讨论(0)
  • 2020-11-28 10:43

    If you need a function try this.
    First we'll create a type:

    CREATE OR REPLACE TYPE T_TABLE IS OBJECT
    (
        Field1 int
        , Field2 VARCHAR(25)
    );
    CREATE TYPE T_TABLE_COLL IS TABLE OF T_TABLE;
    /
    

    Then we'll create the function:

    CREATE OR REPLACE FUNCTION TEST_RETURN_TABLE
    RETURN T_TABLE_COLL
        IS
          l_res_coll T_TABLE_COLL;
          l_index number;
        BEGIN
          l_res_coll := T_TABLE_COLL();
          FOR i IN (
            WITH TAB AS
              (SELECT '1001' ID, 'A,B,C,D,E,F' STR FROM DUAL
              UNION
              SELECT '1002' ID, 'D,E,F' STR FROM DUAL
              UNION
              SELECT '1003' ID, 'C,E,G' STR FROM DUAL
              )
            SELECT id,
              SUBSTR(STR, instr(STR, ',', 1, lvl) + 1, instr(STR, ',', 1, lvl + 1) - instr(STR, ',', 1, lvl) - 1) name
            FROM
              ( SELECT ',' || STR || ',' AS STR, id FROM TAB
              ),
              ( SELECT level AS lvl FROM dual CONNECT BY level <= 100
              )
            WHERE lvl <= LENGTH(STR) - LENGTH(REPLACE(STR, ',')) - 1
            ORDER BY ID, NAME)
          LOOP
            IF i.ID = 1001 THEN
              l_res_coll.extend;
              l_index := l_res_coll.count;
              l_res_coll(l_index):= T_TABLE(i.ID, i.name);
            END IF;
          END LOOP;
          RETURN l_res_coll;
        END;
        /
    

    Now we can select from it:

    select * from table(TEST_RETURN_TABLE()); 
    

    Output:

    SQL> select * from table(TEST_RETURN_TABLE());
    
        FIELD1 FIELD2
    ---------- -------------------------
          1001 A
          1001 B
          1001 C
          1001 D
          1001 E
          1001 F
    
    6 rows selected.
    

    Obviously you'd need to replace the WITH TAB AS... bit with where you would be getting your actual data from. Credit Credit

    0 讨论(0)
提交回复
热议问题