SQL Loader - How do I escape an terminating character - oracle

Friends,
I have had a client data sent to me in that is requested to have fields terminated by '|'
Problem is, some of the filed values also have the "|" as a character that we need to preserve.
Problem rows in my datafile look like this (the '|' in the addres "1|34-36 ..." is the issue)
ID | Address |UPDATEDATE
1423 | 1|34-36 White Street |02/01/199
my .ctl looks like this
options(errors=1000)
load data
into table client_address APPEND
fields terminated by '|'
TRAILING NULLCOLS
(
ID INTEGER EXTERNAL,

If all fields in data file are width-aligned, you could use this .ctl file:
load data
into table client_address APPEND
fields terminated by '~'
TRAILING NULLCOLS
(
line BOUNDFILLER,
ID "to_number(trim(substr(:line,1,8)))",
Address "trim(substr(:line,10,26))",
UPDATEDATE "to_date(trim(substr(:line,37)),'mm/dd/yyyy')"
)
EDIT :
If fields are not width-aligned, but Address is the only field which can contain '|' char inside, then use this .ctl file
load data
into table client_address APPEND
fields terminated by '~'
TRAILING NULLCOLS
(
line BOUNDFILLER,
ID "to_number(trim(regexp_substr(:line,'^[^|]*')))",
Address "trim(regexp_replace(:line,'^[^|]*\|(.*)\|[^|]*$','\1'))",
UPDATEDATE "to_date(trim(regexp_substr(:line,'[^|]*$')),'mm/dd/yyyy')"
)

Related

Can we change Case of input data in SQL LOADER ctl file?

I am trying to insert data from csv to a table using sql loader.
My csv has data in small Caps .But I want the data in the table to be in Large cap for some columns For some Columns it should be InitCap.
INFILE 'abc.csv'
INSERT INTO TABLE T_DETAILS
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS
(
INITCAP(f_name),
UPPER(f_code)```
But i am getting error like
> **SQL*Loader-350: Syntax error at line 16. Expecting "," or ")", found "(".
> INITCAP(f_name),**
> ^
You can, but not like that. Should've been
load data
infile 'abc.csv'
insert
into table t_details
fields terminated by ',' optionally enclosed by '"' trailing nullcols
(f_name "initcap(:f_name)",
f_code "upper(:f_code)"
)

Default value in an Oracle SQL*Loader control file

I have an SQL*Loader control file which is used to transfer data from an XML file to an Oracle database.
My control file is as follows:
options (errors=100,silent=feedback)
load data
infile "dest/TMP-TMP_Employee.xml" "str '</RECORD>'"
replace
into table "TMP_Employee"
TRAILING NULLCOLS
(
DUMMY filler terminated by "<RECORD>",
ID_EMP enclosed by "<ID_EMP>" and "</ID_EMP>",
ORIGINE enclosed by "<ORIGINE>" and "</ORIGINE>",
CODE_TYPE_ORDRE enclosed by "<CODE_TYPE_ORDRE>" and "</CODE_TYPE_ORDRE>",
CODE_STATUS enclosed by "<CODE_STATUS>" and "</CODE_STATUS>",
INDICATOR enclosed by "<INDICATOR>" and "</INDICATOR>"
)
I have added a new column, FULL_TIME, to the TMP_Employee, of type VARCHAR2(1 BYTE).
I need to add the column FULL_TIME in the control file with a default value "0". I added it as follows:
options (errors=100,silent=feedback)
load data
infile "dest/TMP-TMP_Employee.xml" "str '</RECORD>'"
replace
into table "TMP_Employee"
TRAILING NULLCOLS
(
DUMMY filler terminated by "<RECORD>",
ID_EMP enclosed by "<ID_EMP>" and "</ID_EMP>",
ORIGINE enclosed by "<ORIGINE>" and "</ORIGINE>",
CODE_TYPE_ORDRE enclosed by "<CODE_TYPE_ORDRE>" and "</CODE_TYPE_ORDRE>",
CODE_STATUS enclosed by "<CODE_STATUS>" and "</CODE_STATUS>",
INDICATOR enclosed by "<INDICATOR>" and "</INDICATOR>",
IC_REMUNEREE DEFAULT 0
)
But this is not working. Any idea how to do it correctly?

SQL Loader - Use Replace and Append in same control file

As mentioned in the title, i wish to have a control file to handle this case. The scenario is i have to insert record into different table. For example, WHEN (1:3) is HEA, it need to Append into table header. WHEN (1:3) is DTL it need replace into table Detail. is that possible to do this?
I have a situation where data from one file goes to three tables depending on the first field in the file. The WHEN clause looks at the first field and takes action based on that. Notice that when a 'WHEN' is met, the first field is then skipped by declaring it a filler. To answer your question, I believe you can put the APPEND or REPLACE after the INTO TABLE clause. Give it a try and let us know.
OPTIONS (DIRECT=TRUE)
UNRECOVERABLE
LOAD DATA
APPEND
INTO TABLE TABLE_A
WHEN (01) = 'CLM'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
( rec_skip filler POSITION(1)
,CLM_CLAIM_ID CHAR NULLIF(CLM_CLAIM_ID=BLANKS)
...
)
INTO TABLE TABLE_B
WHEN (01) = 'SLN'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
( rec_skip filler POSITION(1)
,SL_CLAIM_ID CHAR NULLIF(SL_CLAIM_ID=BLANKS)
...
)
INTO TABLE TABLE_C
WHEN (01) = 'COB'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
( rec_skip filler POSITION(1)
,COB_CLAIM
...
)
More info: http://docs.oracle.com/cd/B28359_01/server.111/b28319/ldr_control_file.htm#i1005657

how to change the beginning position of the field in sql loader

I have a file which i want to load using sql loader.the sample file is as follows.
1|Deepak|1|raj|Kumar|mcapatna|powerhouse
the control file is
LOAD DATA
INFILE *
TRUNCATE
INTO TABLE Student WHEN (1:1)= '1'
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(
nickName1 position(6) NULLIF nickName1=BLANKS ,
nickName2 NULLIF nickName2=BLANKS ,
class CONSTANT '10',
Address CONSTANT 'NA'
)
The output what i want is nickName1=mcapatna nickName=powerhouse class=10 & Address=NA.
I am getting the value for nickName1=pak & for nickName2= 1
That means it is counting based on sequence of single characters..
The control file is in data file field order. To skip the fields in the data file you do not want, you need to "consume" them by defining them as FILLER.
...
(
skip_1 FILLER,
skip_2 FILLER,
skip_3 FILLER,
skip_4 FILLER,
skip_5 FILLER,
nickname1,
nickname2,
class CONSTANT '10',
Address CONSTANT 'NA'
)
See this fairly recent post for more info: Skipping data fields while loading delimited data using SQLLDR
By adding position(6) in your ctl file, it starts at position 6 till the next |. So pak is the expected value. If you remove position(6), it will start on position 1 automaticly.
I guess something like this, would do the job (this is not tested but gives you a direction):
LOAD DATA
INFILE *
INTO TABLE Student
FIELDS OPTIONALLY ENCLOSED BY '"' TRAILING NULLCOLS
( nickName1 CHAR NULLIF nickName1=BLANKS TERMINATED BY "|"
, nickName2 CHAR NULLIF nickName2=BLANKS TERMINATED BY "|"
, class CHAR CONSTANT '10' TERMINATED BY "|",
, Address CONSTANT 'NA' TERMINATED BY "|"
)

Loading text file in oracle from unix system

I am having text file contaning field in below manner.
"64252368","7489040","305762",
"64285217","12132108","787341",
I am using a below control file.
OPTIONS (SKIP=1)
LOAD DATA
TRUNCATE INTO TABLE test_table
FIELDS TERMINATED BY '",'
(
LEARNEVENT_ID,
ORGANIZATION,
COURSE_ID
)
But, I am getting the error:
Record 1: Rejected - Error on table test_table, column LEARNEVENT_ID
ORA-01722: invalid number
Kindly help me on it.
You need to change your ctl file to include OPTIONALLY ENCLOSED BY option.
OPTIONS (SKIP=1)
LOAD DATA
TRUNCATE INTO TABLE test_table
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
(
LEARNEVENT_ID,
ORGANIZATION,
COURSE_ID
)
I'd recommend reading up on SQL*Loader.
The problem lays with the encapsulation of the numbers with the quotes " " and your fields terminated by '",' simply does not strip the quotes.
Try this
OPTIONS(SKIP=1)
LOAD DATA
TRUNCATE INTO TABLE test_table
FIELDS TERMINATED BY ','
TRAILING NULLCOLS
(
LEARNEVENT_ID "replace ( :LEARNEVENT_ID ,'"', '')",
ORGINAZATION "replace ( :ORGINAZATION ,'"', '')",
COURSE_ID "replace ( :COURSE_ID ,'"', '')"
)

Resources