Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Escape commas in column field in CSV

user5108636Sep 12 2016 — edited Sep 12 2016

Hi All,

     I get data from a table and parse it and write into csv in a procedure. However, when the column data station has a comma separated value say ' Willman, 107.1', the csv parsing fails. How to escape commas in the column data when parsing to csv.

Please suggest,

CREATE OR REPLACE PACKAGE BODY TEST.csv_pk

AS

PROCEDURE write_csv (filename VARCHAR2)

   IS

      v_file UTL_FILE.FILE_TYPE;

      CURSOR c_stats (cp_last_run_datetime    DATE) IS

      SELECT rs.agent,

             rs.station,

             rs.level

      FROM r_stats rs

      WHERE rs.record_date BETWEEN cp_last_run_datetime AND SYSDATE;

      cr_stat c_stats%ROWTYPE;

   BEGIN

         v_file := UTL_FILE.FOPEN('CSV', 'STATS.csv', 'w', 32767);

         FOR cr_stat IN c_stats(TO_DATE('28/10/2015', 'DD/MM/YYYY'))

         LOOP

             UTL_FILE.PUT(v_file, ',' || cr_stat.agent);

             UTL_FILE.PUT(v_file, ',' || cr_stat.station);  --csv parsing fails when data is ' Willman, 107.1'

             UTL_FILE.PUT(v_file, ',' || cr_stat.level);

             UTL_FILE.NEW_LINE(v_file);

         END LOOP;

         UTL_FILE.FCLOSE(v_file);

   END write_csv;

  

END csv_pk;

This post has been answered by Solomon Yakobson on Sep 12 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 10 2016
Added on Sep 12 2016
5 comments
3,681 views