I am loading a .csv file data into oracle table through sql loader. One of the fields has a new line character (CRLF) in its data and so, am getting the below error:
second enclosure string not present
This is my control file
load data
characterset UTF8
infile 'C:\Users\lab.csv'
truncate
into table test_labinal
fields terminated by ";" optionally enclosed by '"'
TRAILING NULLCOLS
(
STATEMENT_STATUS ,
MANDATORY_TASK ,
COMMENTS CHAR(9999) "SubStr(:Comments, 0, 1000)"
)
The field COMMENTS has a new line character in one of its records. Can any one suggest a solution for this.
Thanks
Note: The
CHR(13)
is the ASCII character for "carriage return" and theCHR(10)
is the ASCII character for "new line". Using the Oracle PL/SQLREPLACE
command without a replacement value will remove any "carriage return" and/or "new line" character that is embedded in your data. Which is probably the case because the comment field is the last field in your CSV file.I found the best way to load the .csv files with fields containing newline and comma.Please run the macro over the .csv file and then load using sqlloader
It's running perfect for me.
You can use
replace(replace(column_name, chr(10)), chr(13))
to remove newline charactors orregexp_replace(column_name, '\s+')
to remove non printable charactors during loadingIf your last field is always present (though
trailing nullcols
suggests it isn't) and you have some control over the formatting, you can use theCONTINUEIF
directive to treat the second line as part of the same logical record.If the
comments
field is always present and enclosed in double-quotes then you can do:Which would handle data records like:
Or if you always have a delimiter after the comments field, whether it is populated or not:
Which would handle:
Both ways will load the data as:
But this loses the new line from the data. To keep that you need the terminating field delimiter to be present, and instead of
CONTINUEIF
you can change the record separator using the stream record format:The
"str ';\n'"
defines the terminator as the combination of the field terminator and a new line character. Your split comment only has that combination on the final line. With the same data file as the previous version, this gives:Since you're on Windows you might have to include
\r
in the format as well, e.g."str ';\r\n'"
, but I'm not able to check that.