正则表达式删除Oracle 11.2g中引号之间的逗号

时间:2017-12-14 10:58:18

标签: oracle oracle11g oracle-sqldeveloper

我的代码是:

set serveroutput on size unlimited;
DECLARE
  v_line_unclean   VARCHAR2(32767); -- 32767 BYTES
  v_line_clean     VARCHAR2(32767);
  v_clean_val     VARCHAR2(32767);
  SQLSMT           VARCHAR2(32767);
  v_line_in_record INTEGER;
  pattern varchar2(15) := '("[^"]*"|[^,]+)';
  v_name           VARCHAR2(50);
  v_first_column   VARCHAR2(200);
  EMP_FILE UTL_FILE.FILE_TYPE;
BEGIN
  DBMS_OUTPUT.ENABLE(9000000);
  EMP_FILE         := UTL_FILE.FOPEN('EGIS_FILE_DIR','TEST.csv','R', 32767); -- open the file from oracle directory
  v_line_in_record := 0;                                         --we skip the first line
  IF UTL_FILE.IS_OPEN(EMP_FILE) THEN
    LOOP
      v_line_in_record := v_line_in_record + 1;
      --DBMS_OUTPUT.PUT_LINE(v_line_in_record);
      BEGIN
        UTL_FILE.GET_LINE(EMP_FILE,v_line_unclean);
        IF v_line_in_record = 1 THEN-- first record here
          DBMS_OUTPUT.PUT_LINE('');
        ELSIF v_line_in_record = 2 THEN-- second record here (header)
          DBMS_OUTPUT.PUT_LINE('');
          DBMS_OUTPUT.PUT_LINE(v_line_unclean);
          v_first_column   := SUBSTR(v_line_unclean,1,instr(v_line_unclean,',',10,1)-1);
          dbms_output.put_line('1st '||REGEXP_SUBSTR(v_line_unclean, '[^,]+', 1, 1));
        ELSE -- body records here);
          SELECT REPLACE(v_line_unclean,'((\)|^).*?(\(|$))|,', '\1')INTO v_line_clean FROM DUAL;
          SQLSMT := 'INSERT INTO SITE_CONFIG_2G VALUES('''||v_line_clean||''')';
          EXECUTE IMMEDIATE SQLSMT;
        END IF;
        COMMIT;
        DBMS_OUTPUT.PUT_lINE(SQLSMT);
      EXCEPTION
      WHEN NO_DATA_FOUND THEN
        DBMS_OUTPUT.PUT_lINE('NO DATA FOUND EXCEPTION');
        EXIT;
      WHEN TOO_MANY_ROWS THEN
        DBMS_OUTPUT.PUT_lINE('TO MANY ROW EXCEPTION');
        EXIT;
      WHEN OTHERS THEN
        DBMS_OUTPUT.PUT_LINE(sqlcode||sqlerrm);
        ROLLBACK;
        EXIT;
      END;--EXCEPTION
    END LOOP;
  END IF;
  UTL_FILE.FCLOSE(EMP_FILE);
END;

结果

INSERT INTO SITE_CONFIG_2G VALUES('1,gold,benz,2018,1,blue,"34,000,000",6.4,new,CSV')
INSERT INTO SITE_CONFIG_2G VALUES('2,silver,bmw,2016,1,silver,"51,000,000",6.5,New,CSV')
INSERT INTO SITE_CONFIG_2G VALUES('3,bronze,range,2017,1,light-blue,"24,000,000",7.8,New,RVS')

我想删除"24,000,000"中引号之间的逗号,以便给我"24000000"

目前的结果是:

3,bronze,range,2017,1,light-blue,"24,000,000",7.8,New,RVS

预期结果是:

3,bronze,range,2017,1,light-blue,"24000000",7.8,New,RVS

1 个答案:

答案 0 :(得分:0)

你可以尝试一下。

    select regexp_replace('1,gold,benz,2018,1,blue,"34,000,000",6.4,new,CSV',
'(")([^"|,]+)(,)([^"|,]+)(,)([^"|,]+)(")',
'\1\2\4\6\7') from dual;