简体   繁体   中英

Split comma separated string with Oracle

I have a query that pulls the content of a text separated by comma exactly as this:

INSERVICE JOB #: N19020200001 
SERVICE_CENTER:SBY,OH_CIRCUIT:MALTA8501,CREW:3675,URD_PRINT:STG-123/S1,FEEDER:PFB969,ISOLATED_1:SCC-1-B969,ISOLATED_2:UDTB969-5,RECONDUCTOR:Y,JACKETED_CABLE:N,CABLE_CART:N,LIVE_FRONT:N,BOOM:null,BACK_HOE:null,EASY_HAULING:null

Is there a way in SQL I can select/partition it to be in separate fields as below (it is always consistent as above):

在此处输入图片说明

I have a custom string parser function you can use ...

NOTE: For headers, you can replace them like REPLACE('HEADER','') and get values with commas...

You can check below code and use it in your db:

  FUNCTION STRING_PARSER(VAL VARCHAR2, POSITION VARCHAR2, DELIMITER VARCHAR2) RETURN VARCHAR2 IS
    v_pos3 number;
    v_pos4 number;
  BEGIN

    /* Return 3rd occurrence of '_' */
    v_pos3 := INSTR(VAL, DELIMITER, 1, POSITION) + 1;

    /* Return 4rd occurrence of '_' */
    v_pos4 := INSTR(VAL, DELIMITER, 1, POSITION + 1);

    return SUBSTR(VAL, v_pos3, v_pos4 - v_pos3);

  END;

Usage :

select report_tools_pkg.string_parser(',1,2,3',2,',') from dual

Note: Add a ',' || columnname to your sql if you want to you use ,it as it is ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM