字符串拆分

  • A+
所属分类:BLOG Database Oracle SQL

因某个需求,写了一个字符串拆分,可适配多种分隔符

用法如下:

select * from table(GET_MED_OVAL_FILE_INFO('123----1----a1####345----11----a2',null,null));
select * from table(GET_MED_OVAL_FILE_INFO('123-1-a1#345-11-a2','#','-'));

代码内容如下:

FUNCTION GET_MED_OVAL_FILE_INFO
  /*******************************************************************************
     Object Name : get_med_oval_file_info
     Writer      : wangzhengshun
     Write Date  : 2018/11/2
     Usage       : 解析附件信息
    *******************************************************************************/
  (IN_FILE_INFO       IN VARCHAR2,
   IN_REGXP_SPLIT_ROW IN VARCHAR2,
   IN_REGXP_SPLIT_COL IN VARCHAR2) RETURN TYT_MED_OVAL_FILE_INFO
    PIPELINED IS
    LV_FILE_ID   VARCHAR2(40);
    LV_CMS_ID    VARCHAR2(40);
    LV_FILE_SEQ  VARCHAR2(10);
    LV_FILE_DESC VARCHAR2(200);
    LV_FILE_INFO VARCHAR2(4000);
    T_FILE_INFO  VARCHAR2(4000);

    CNT    NUMBER;
    T_CNT  NUMBER;
    LV_CNT NUMBER;

    REGXP_SPLIT_COL VARCHAR2(10);
    REGXP_SPLIT_ROW VARCHAR2(10);

  BEGIN

    --行分隔符
    IF IN_REGXP_SPLIT_ROW IS NULL THEN
      REGXP_SPLIT_ROW := '####';
    ELSE
      REGXP_SPLIT_ROW := IN_REGXP_SPLIT_ROW;
    END IF;
    --列分隔符
    IF IN_REGXP_SPLIT_COL IS NULL THEN
      REGXP_SPLIT_COL := '----';
    ELSE
      REGXP_SPLIT_COL := IN_REGXP_SPLIT_COL;
    END IF;

    T_FILE_INFO := IN_FILE_INFO || REGXP_SPLIT_ROW;
    CNT         := REGEXP_COUNT(T_FILE_INFO, REGXP_SPLIT_ROW);

    T_CNT := 0;

    FOR X IN 1 .. CNT LOOP

      LV_CNT := REGEXP_INSTR(T_FILE_INFO, REGXP_SPLIT_ROW, 1, X) +
                LENGTH(REGXP_SPLIT_ROW) - 1;

      LV_FILE_INFO := SUBSTR(T_FILE_INFO,
                             T_CNT + 1,
                             LV_CNT - T_CNT - LENGTH(REGXP_SPLIT_ROW));

      LV_FILE_ID  := SUBSTR(LV_FILE_INFO,
                            1,
                            REGEXP_INSTR(LV_FILE_INFO, REGXP_SPLIT_COL, 1, 1) - 1);
      LV_FILE_SEQ := SUBSTR(LV_FILE_INFO,
                            REGEXP_INSTR(LV_FILE_INFO, REGXP_SPLIT_COL, 1, 1) +
                            LENGTH(REGXP_SPLIT_ROW),
                            REGEXP_INSTR(LV_FILE_INFO, REGXP_SPLIT_COL, 1, 2) -
                            REGEXP_INSTR(LV_FILE_INFO, REGXP_SPLIT_COL, 1, 1) -
                            LENGTH(REGXP_SPLIT_ROW));

      LV_FILE_DESC := SUBSTR(LV_FILE_INFO,
                             REGEXP_INSTR(LV_FILE_INFO,
                                          REGXP_SPLIT_COL,
                                          1,
                                          2) + LENGTH(REGXP_SPLIT_ROW));

      LV_CMS_ID := LV_FILE_ID;

      PIPE ROW(TYR_MED_OVAL_FILE_INFO(LV_FILE_ID,
                                      LV_FILE_SEQ,
                                      LV_FILE_DESC));

      T_CNT := REGEXP_INSTR(IN_FILE_INFO, REGXP_SPLIT_ROW, 1, X) +
               LENGTH(REGXP_SPLIT_ROW) - 1;

    END LOOP;

  END GET_MED_OVAL_FILE_INFO;

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: