Skip to Main Content
  • Questions
  • How to insert the data using sql*loader by CSV file which contain comma as separator and comma present at column value

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, SWARUP KUMAR.

Asked: November 14, 2016 - 2:25 pm UTC

Last updated: November 15, 2016 - 1:52 am UTC

Version: Oracle 11g

Viewed 10K+ times! This question is

You Asked

Hi Connor,

I have an issue with sql*loader during loading below CSV file

I have a csv file with below data:-

Column names:-
empid,empname,address,salary,deptn0

CSV file data:-

1123,Swarup,PO Box 42,1407 Graymalkin Lane,Salem Center, New York 10560,10000,10
1124,Kumar,PO Box 1492,16346 E. Graham Circle,Palmer, AK 99645,20000,20

Row 1:-
empid:-1123
adress:-Swarup
address:-(PO Box 42,1407 Graymalkin Lane,Salem Center, New York 10560)
Salary:-10000
deptno:-10

Row 2:-
empid:-1124
adress:-Kumar
address:-(PO Box 1492,16346 E. Graham Circle,Palmer, AK 99645)
Salary:-20000
deptno:-20

when i have tried to write the control file i was getting difficulty when it came to address column
As my address column contain ','(comma) which was present as a separator in my csv file

So could you please help me how i will write the control file for above scenario so that i will insert the address column data without any error

Thanks & Regards
Swarup-









and Connor said...

SQL> create table t ( empid int,empname varchar2(20),address varchar2(200),salary int,deptno int );

Table created.

SQL> host cat x.ctl
LOAD DATA
INFILE *
INTO TABLE T
TRUNCATE
FIELDS TERMINATED BY ','
(
empid    position(1:1000) "substr(:empid,1,instr(:empid,',')-1)",
empname  position(1:1)   "substr(:empid,instr(:empid,',')+1, instr(:empid,',',1,2)-instr(:empid,',')-1)",
address  position(1:1)   "substr(:empid,instr(:empid,',',1,2)+1, instr(:empid,',',-1,2)-instr(:empid,',',1,2)-1)",
salary   position(1:1)   "substr(:empid,instr(:empid,',',-1,2)+1, instr(:empid,',',-1)-instr(:empid,',',-1,2)-1)",
deptno   position(1:1)   "substr(:empid,instr(:empid,',',-1)+1)"
)
BEGINDATA
1123,Swarup,PO Box 42,1407 Graymalkin Lane,Salem Center, New York 10560,10000,10
1124,Kumar,PO Box 1492,16346 E. Graham Circle,Palmer, AK 99645,20000,20
1125,Kumar,Simple Address No Commas,20000,20
1126,Kumar,Simple Address One,Comma,20000,20

SQL> host sqlldr userid=/ control=x

SQL*Loader: Release 12.1.0.2.0 - Production on Tue Nov 15 09:51:16 2016

Copyright (c) 1982, 2016, Oracle and/or its affiliates.  All rights reserved.

Path used:      Conventional
Commit point reached - logical record count 4

Table T:
  4 Rows successfully loaded.

Check the log file:
  x.log
for more information about the load.

SQL> select * from t;

     EMPID EMPNAME              ADDRESS                                                          SALARY     DEPTNO
---------- -------------------- ------------------------------------------------------------ ---------- ----------
      1123 Swarup               PO Box 42,1407 Graymalkin Lane,Salem Center, New York 10560       10000         10
      1124 Kumar                PO Box 1492,16346 E. Graham Circle,Palmer, AK 99645               20000         20
      1125 Kumar                Simple Address No Commas                                          20000         20
      1126 Kumar                Simple Address One,Comma                                          20000         20

SQL>



Is this answer out of date? If it is, please let us know via a Comment