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!

Commas in values issue during CSV Import

DooliusJun 25 2014 — edited Jul 1 2014

I'm having an issue with my code that inports and parses through a CSV file failing if there is a comma within the value for a column.

Is there a way to distinguish between a comma that separates a column and a comma that is within the column value?

The code below works great unless there is a comma within a value (which there is in the CHY_NAME value).

DECLARE 
v_blob_data       BLOB; 
v_blob_len        NUMBER; 
v_position        NUMBER; 
v_raw_chunk       RAW(10000); 
v_char      CHAR(1); 
c_chunk_len   number       := 1; 
v_line        VARCHAR2 (32767)        := NULL; 
v_data_array      wwv_flow_global.vc_arr2; 
v_rows number; 
v_sr_no number := 1;
v_first_line_done boolean := false;
v_error_cd number :=0; 
BEGIN 
delete from TEMP_UPDATE where username = :P1_USER_ID; 
-- Read data from wwv_flow_files</span> 
select 
blob_content 
into v_blob_data 
from wwv_flow_files 
where name = :P2_FILE_UPLOAD; 
v_blob_len := dbms_lob.getlength(v_blob_data); 
v_position := 1; 
-- Read and convert binary to char</span>
WHILE ( v_position <= v_blob_len ) 
LOOP 
v_raw_chunk := dbms_lob.substr(v_blob_data,c_chunk_len,v_position); 
v_char :=  chr(hex_to_decimal(rawtohex(v_raw_chunk))); 
v_line := v_line || v_char; 
v_position := v_position + c_chunk_len; 
-- When a whole line is retrieved </span>
IF v_char = CHR(10) THEN 
  -- Convert comma to : to use wwv_flow_utilities </span> 
  v_line := REPLACE (v_line, ',', ':');
  v_line := REPLACE (trim(v_line), '-', NULL);
  -- Convert each column separated by : into array of data </span> 
  v_data_array := wwv_flow_utilities.string_to_table (v_line);
  -- Insert data into target table </span>
  IF(v_first_line_done != true)THEN
   v_first_line_done := true;
   IF(v_data_array(1) LIKE '%USERNAME%' AND
   v_data_array(2) LIKE '%PCFN%' AND
   v_data_array(3) LIKE '%TCN%') THEN
    v_error_cd := 0;
    v_line := NULL;
   ELSE
    v_error_cd := 1;
   END IF;
  ELSIF(v_first_line_done = true AND v_error_cd = 0) THEN 
   EXECUTE IMMEDIATE 'insert into TEMP_UPDATE
   (
   USERNAME,
   CHY_ATA_DATE,
   CHY_ATD_DATE,
   CHY_NAME,
   UPDATED_BY)
   values (:1,:2,:3,:4,:5)'
   USING
   v_data_array(1),
   v_data_array(2),
   v_data_array(3),
   v_data_array(4),
   v_data_array(5); 
   -- Clear out 
   v_line := NULL; v_sr_no := v_sr_no + 1;
  END IF;
END IF; 
END LOOP;
DELETE FROM WWV_FLOW_FILES where name = :P2_FILE_UPLOAD;
IF(v_error_cd = 1) THEN
INSERT INTO temp_update (USERNAME, ERROR_DESC)
VALUES (:P1_USER_ID, 'Spreadsheet column order incorrect.');
END IF;

END;

What happens is, for example:

IF:

CHY_NAME = Hello, There

UPDATED_BY = Me

When the data is put into the table:

CHY_NAME = Hello

UPDATED_BY = There

I've noticed that when in excel, if you type something in a column that has a column it puts quotation marks around it ("Hello, There")

In the line of the code that says:

v_line := REPLACE (v_line, ',', ':');

Is there a way to write it as:

v_line := REPLACE (v_line, ',', ':')

     where v_line not like '%"%,%"%'

Or is there a better way to ignore commas that are between quotation marks?

thanks

Steven

This post has been answered by Doolius on Jun 25 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 29 2014
Added on Jun 25 2014
11 comments
5,496 views