My raw file like below
bbbb 1000 Open 1000 Dep 12/03/2010
dddd 1001 Open 2000 Loan 13/01/2019
eeee 1003 Dor 3000 Dep 11/04/1965
Sqlldr control file
load data
infile *
truncate into table Mytab
fields
trailing nullcols
(
Name position (1:4),
Acc position(6:9),
Status Position(11:14),
Amt position(15:18),
type position(20:23),
Date position(24:33)
)
begin data
I need date column with hyphen(-) symbol not for slash(/) symbol.
12/03/2010 -- 12-03-2010
Date position(24:33) ---> What I have to mention here
It would help if you also provided mytab description. Because, "Date" column (whose name is invalid; it is reserved for datatype name. I renamed it to "Datum") should be of date datatype, while it seems you use varchar2. In either case, you'd use appropriate function, enclosed into double quotes. I used replace as it does what you asked for.
Here's an example:
Control file (yours is invalid as it doesn't follow data format; I fixed it):
load data
infile *
truncate into table Mytab
fields
trailing nullcols
(Name position(1:4),
Acc position(6:9),
Status position(11:14),
Amt position(16:19),
type position(21:24),
Datum position(26:35) "replace(:datum, '/', '-')"
)
begindata
bbbb 1000 Open 1000 Dep 12/03/2010
dddd 1001 Open 2000 Loan 13/01/2019
eeee 1003 Dor 3000 Dep 11/04/1965
Loading session & result:
SQL> $sqlldr scott/tiger#xe control=test34.ctl log=test34.log
SQL*Loader: Release 11.2.0.1.0 - Production on Sri O×u 24 07:54:46 2021
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 3
SQL> select * from mytab;
NAME ACC STATUS AMT TYPE DATUM
---------- ---------- ---------- ---------- ---------- ----------
bbbb 1000 Open 1000 Dep 12-03-2010
dddd 1001 Open 2000 Loan 13-01-2019
eeee 1003 Dor 3000 Dep 11-04-1965
SQL>
Related
I am trying to load a '|' delimited file, but it fails because some columns have crlf values.
I converted the text files to xlsx and imported them successfully using SQL Developer.
I noticed that SQL Developer using a Line Terminator option set to "standard: CR LF, CR or LF".
I suspect that I need to set that in my ctl file, but have been unable to find the correct syntax.
Any assistance would be appreciated.
Here is a screenshot from SQL Developer:
As usual, it helps if you post what you have (in this case, control file, table description and sample data). Without it, we have to guess and that doesn't have to reflect reality.
Sample table:
SQL> create table test
2 (id number,
3 description varchar2(50));
Table created.
Control file (contains sample data):
load data
infile *
replace
continueif next preserve(1:1) != "|"
into table test
fields terminated by '|'
trailing nullcols
(
dummy filler,
id,
description
)
begindata
|1|this is some text
with no meaning
at all
|2|some
more text
Loading session:
SQL> $sqlldr scott/tiger#pdb1 control=test11.ctl log=test11.log
SQL*Loader: Release 21.0.0.0.0 - Production on Sat Nov 26 21:51:53 2022
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle and/or its affiliates. All rights reserved.
Path used: Conventional
Commit point reached - logical record count 1
Commit point reached - logical record count 2
Table TEST:
2 Rows successfully loaded.
Check the log file:
test11.log
for more information about the load.
Result:
SQL> select * from test;
ID DESCRIPTION
---------- --------------------------------------------------
1 this is some text with no meaning at all
2 some more text
SQL>
[EDIT] Using piece of code #p3consulting suggested: no difference (at least, not in my 21XE database):
Control file (modified line #2, the infile one):
load data
infile * "STR X'220D0A'"
replace
continueif next preserve(1:1) != "|"
into table test
fields terminated by '|'
trailing nullcols
(
dummy filler,
id,
description
)
begindata
|1|this is some text
with no meaning
at all
|2|some
more text
Loading session:
SQL> $sqlldr scott/tiger#pdb1 control=test11.ctl log=test11.log
SQL*Loader: Release 21.0.0.0.0 - Production on Sun Nov 27 12:34:04 2022
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle and/or its affiliates. All rights reserved.
SQL*Loader-283: file processing string "STR X'220D0A'" ignored for INFILE *
Path used: Conventional
Commit point reached - logical record count 1
Commit point reached - logical record count 2
Table TEST:
2 Rows successfully loaded.
Check the log file:
test11.log
for more information about the load.
Result: no difference:
SQL> select * from test;
ID DESCRIPTION
---------- --------------------------------------------------
1 this is some text with no meaning at all
2 some more text
SQL>
[EDIT #2: removed CONTINUEIF]
load data
infile * "STR X'220D0A'"
replace
into table test
<snip>
begindata
<snip>
Loading session:
SQL> $sqlldr scott/tiger#pdb1 control=test11.ctl log=test11.log
SQL*Loader-283: file processing string "STR X'220D0A'" ignored for INFILE *
<snip>
Ah! I should've read the message, Oracle says it all:
file processing string "STR X'220D0A'" ignored for INFILE
So: with a new control file:
load data
infile "c:\temp\test11.txt" "STR X'220D0A'"
replace
into table test
fields terminated by '|'
trailing nullcols
(
dummy filler,
id,
description
)
and data in a separate, test11.txt file:
|1|this is some text
with no meaning
at all
|2|some
more text
Loading session:
<snip>
Commit point reached - logical record count 1
Table TEST:
1 Row successfully loaded.
Check the log file:
test11.log
for more information about the load.
Result:
SQL> select * from test;
ID DESCRIPTION
---------- --------------------------------------------------
1 this is some text
with no meaning
at all
SQL>
That looks A LOT better now!
Though, row#2 is missing. Log file doesn't say anything about it:
SQL> $type test11.log
SQL*Loader: Release 21.0.0.0.0 - Production on Sun Nov 27 15:30:39 2022
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle and/or its affiliates. All rights reserved.
Control File: test11.ctl
Data File: c:\temp\test11.txt
File processing option string: "STR X'220D0A'"
Bad File: test11.bad
Discard File: none specified
(Allow all discards)
Number to load: ALL
Number to skip: 0
Errors allowed: 50
Bind array: 250 rows, maximum of 1048576 bytes
Continuation: none specified
Path used: Conventional
Table TEST, loaded from every logical record.
Insert option in effect for this table: REPLACE
TRAILING NULLCOLS option in effect
Column Name Position Len Term Encl Datatype
------------------------------ ---------- ----- ---- ---- ---------------------
DUMMY FIRST * | CHARACTER
(FILLER FIELD)
ID NEXT * | CHARACTER
DESCRIPTION NEXT * | CHARACTER
Table TEST:
1 Row successfully loaded.
0 Rows not loaded due to data errors.
0 Rows not loaded because all WHEN clauses were failed.
0 Rows not loaded because all fields were null.
Space allocated for bind array: 129000 bytes(250 rows)
Read buffer bytes: 1048576
Total logical records skipped: 0
Total logical records read: 1
Total logical records rejected: 0
Total logical records discarded: 0
Run began on Sun Nov 27 15:30:39 2022
Run ended on Sun Nov 27 15:30:39 2022
Elapsed time was: 00:00:00.17
CPU time was: 00:00:00.11
SQL>
No bad nor discard file:
SQL> $type test11.bad
The system cannot find the file specified.
SQL> $del test11.dsc
Could Not Find c:\temp\test11.dsc
SQL>
The input file looks like this (when all characters are displayed):
[FINALLY]
p3consulting: 22 is not the hex for | ... it's 7C... but you need to finish the record by |CRLF... your rows end with only CRLF.
Bingo!
Control file:
load data
infile "c:\temp\test11.txt" "STR X'7C0D0A'"
replace
into table test
fields terminated by '|'
trailing nullcols
(
dummy filler,
id,
description
)
Test11.txt:
|1|this is some text
with no meaning
at all|
|2|some
more text|
Loading:
SQL> $sqlldr scott/tiger#pdb1 control=test11.ctl log=test11.log
SQL*Loader: Release 21.0.0.0.0 - Production on Sun Nov 27 17:11:21 2022
Version 21.3.0.0.0
Copyright (c) 1982, 2021, Oracle and/or its affiliates. All rights reserved.
Path used: Conventional
Commit point reached - logical record count 2
Table TEST:
2 Rows successfully loaded.
Check the log file:
test11.log
for more information about the load.
Result:
SQL> select * From test;
ID DESCRIPTION
---------- --------------------------------------------------
1 this is some text
with no meaning
at all
2 some
more text
SQL>
Try
INFILE 'target.dat' "STR X'220D0A'"
The terminator_string is specified as either 'char_string' or X'hex_string' where:
'char_string' is a string of characters enclosed in single or double quotation marks
X'hex_string' is a byte string in hexadecimal format that you can get with the cast_to_raw function.
So here X'220D0A' is "|CRLF" and this will keep the "CRLF" in the imported column.
using the control file below to load data
LOAD DATA
INFILE '/c/Transaction.txt'
INTO TABLE tab1 APPEND WHEN (1:1) = 'D'
(RUN_ID "RUN_ID_SEQ.NEXTVAL"
,RUN_DATE_TIME "SYSDATE"
)
INTO TABLE tab2 TRUNCATE WHEN(1:1) <> 'D'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
(
DEALER_NUMBER
,TRAN_CODE
,TRAN_AMOUNT "TO_NUMBER(:PL818_TRAN_AMOUNT,'999999.99')"
,TRAN_DATE "TO_DATE(:PL818_TRAN_DATE,'DD-MM-YYYY')"
)
This is the data set
DEALER_ID|TRAN_TYPE_CODE|TRAN_AMOUNT|TRAN_DATE
203113|34|1000.50|12-07-2022
No errors during load. Data loads correctly into the first table but first two characters are skipped when loading data into second table, table data looks like this. What could be causing this?
3113 34 1000.5 12-JUL-22
Skips "20"
Sample tables:
SQL> desc tab1
Name Null? Type
----------------------------------------- -------- ----------------------------
RUN_ID NUMBER
RUN_DATE_TIME DATE
SQL> desc tab2
Name Null? Type
----------------------------------------- -------- ----------------------------
DEALER_NUMBER NUMBER
TRAN_CODE NUMBER
TRAN_AMOUNT NUMBER
TRAN_DATE DATE
SQL>
Control file; note position(1) when loading into tab2. You must use it when loading data into different tables, using the same control file:
LOAD DATA
INFILE *
INTO TABLE tab1 APPEND WHEN (1:1) = 'D'
(RUN_ID "RUN_ID_SEQ.NEXTVAL"
,RUN_DATE_TIME "SYSDATE"
)
INTO TABLE tab2 TRUNCATE WHEN(1:1) <> 'D'
FIELDS TERMINATED BY '|' TRAILING NULLCOLS
(
DEALER_NUMBER position(1) --> here
,TRAN_CODE
,TRAN_AMOUNT "TO_NUMBER(:TRAN_AMOUNT,'999999.99')"
,TRAN_DATE "TO_DATE(:TRAN_DATE,'DD-MM-YYYY')"
)
BEGINDATA
203113|34|1000.50|12-07-2022
Loading session and the result:
SQL> $sqlldr scott/tiger#orcl control=test42.ctl log=test42.log
SQL*Loader: Release 18.0.0.0.0 - Production on Sri Srp 13 08:28:19 2022
Version 18.5.0.0.0
Copyright (c) 1982, 2018, Oracle and/or its affiliates. All rights reserved.
Path used: Conventional
Commit point reached - logical record count 1
Table TAB1:
0 Rows successfully loaded.
Table TAB2:
1 Row successfully loaded.
Check the log file:
test42.log
for more information about the load.
SQL> select * from tab2;
DEALER_NUMBER TRAN_CODE TRAN_AMOUNT TRAN_DATE
------------- ---------- ----------- ----------
203113 34 1000,5 12-07-2022
--
here's your missing "20"
SQL>
I am trying to load a tsv file with variable length and blanks but I cannot load it.
My TSV file has:
code name information surname
1234 Peter Peter
1111 Carl exampleexample example Jhon
I'm trying with:
OPTIONS (SKIP=1)
LOAD DATA
INFILE 'EXAMPLE.TSV'
INTO TABLE PERSON
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
(
CODE,
NAME,
INFORMATION,
SURNAME
)
Here's an example.
SQL> desc person
Name Null? Type
----------------------------------------- -------- ----------------------------
CODE NUMBER
NAME VARCHAR2(10)
INFORMATION VARCHAR2(30)
SURNAME VARCHAR2(10)
SQL>
Control file; note fields terminated by x'09'. I included sample data into the control file itself, for simplicity.
options (skip=1)
load data
infile *
replace
into table person
fields terminated by x'09'
(
code,
name,
information,
surname
)
begindata
code name information surname
1234 Peter Peter
1111 Carl example exa John
Testing:
SQL> $sqlldr scott/tiger control=test18.ctl log=test18.log
SQL*Loader: Release 11.2.0.2.0 - Production on Sri Tra 29 20:26:06 2020
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 2
SQL> select * From person;
CODE NAME INFORMATION SURNAME
---------- ---------- ------------------------------ ----------
1234 Peter Peter
1111 Carl example exa John
I am loading CSV file data into table called EMPLOYEE using SQL*Loader.
My CSV file data is separated by pipe (|):
EMPID|EMPNAME_ADDRESS|SALARY|GRADE
123|Rams Hyd|1000|A1
124|Sand MUM|2,000|A2
125|"PRASANNA qwer trasf\"501 advv vvd, qee ggg\trfe \411005 THE|3,00,000|A3
and my control file is:
LOAD DATA
Insert INTO TABLE EMPLOYEE
Fields terminated by "|" Optionally enclosed by '"' TRAILING NULLCOLS
(
EMPID,
EMPNAME,
SALARY,
GRADE
)
When I load the data using above control file, the first two record are loading fine and for the third record i am getting error like as mentioned below.
no terminator found after TERMINATED and ENCLOSED field
Please suggest changes to be done to load the data properly.
Here's a way to load such data.
First, a target table (I hope it makes sense; you should have provided it):
SQL> create table employee
2 (empid number,
3 empname varchar2(200),
4 salary number,
5 grade varchar2(10)
6 );
Table created.
SQL>
Control file: note the SKIP option (which skips the header line), as well as REPLACE function call for the SALARY column (to remove superfluous commas which are formatting stuff; add them later, in the presentation layer).
options (skip = 1)
load data
infile *
replace
into table employee
fields terminated by "|" TRAILING NULLCOLS
(
empid,
empname,
salary "replace(:salary, ',', null)",
grade)
begindata
EMPID|EMPNAME_ADDRESS|SALARY|GRADE
123|Rams Hyd|1000|A1
124|Sand MUM|2,000|A2
125|"PRASANNA qwer trasf\"501 advv vvd, qee ggg\trfe \411005 THE|3,00,000|A3
Loading session:
SQL> $sqlldr scott/tiger control=test06.ctl log=test06.log
SQL*Loader: Release 11.2.0.2.0 - Production on Uto Tra 9 19:37:46 2019
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 2
Commit point reached - logical record count 3
SQL> select * From employee;
EMPID EMPNAME SALARY GRADE
---------- ---------------------------------------- ---------- ----------
123 Rams Hyd 1000 A1
124 Sand MUM 2000 A2
125 "PRASANNA qwer trasf\"501 advv vvd, qee 300000 A3
ggg\trfe \411005 THE
SQL>
I have the following issue using SQL Loader to load a small table, my process populates a field which can be populated for possitive or negative decimal numbers, and the problem is that when the number is possitive, the process rounds with 2 positions, but if it's negative only use 1 position. I need 2 position, and this is the simple code:
UNRECOVERABLE LOAD DATA
TRUNCATE
INTO TABLE [SCHEMA].TABLE1
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
FIELD1 "trim(:FIELD1 )"
)
Example:
Source: 12345,78 Target: 1234,78
Source: -12345,78 Target: -1234,8
Edit1: It just happend with number with 7 or more integer length (X < -1000000), but I can insert this numbers with regular insert instead of SQLLoader.
Edit2: I have noticed that it is not a problem with sql*loader because the .ctl is already wrong. This file is generated with python and SQL with this properties:
set colsep |
set headsep off
set pagesize 0
set trimspool on
set linesize 10000
set termout off
set feedback off
set arraysize 250
But I dont know where it is defined the lenght of each field, and this looks the error.
I'd say that you're not looking correctly. Data is, probably, stored OK, but you don't see it. Here's an example:
SQL> create table test (field1 number);
Table created.
SQL>
Control file:
load data
infile *
replace into table test
fields terminated by '|' trailing nullcols
(
field1
)
begindata
12345,78
-12345,78
987654321
-987654321
987654321,01234
-987654321,01234
111333444,887766
-112333444,887766
Loading session & the result:
SQL> $sqlldr scott/tiger#orcl control=test26.ctl log=test26.log
SQL*Loader: Release 11.2.0.2.0 - Production on Uto Kol 21 08:52:10 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 7
Commit point reached - logical record count 8
SQL> select * from test;
FIELD1
----------
12345,78
-12345,78
987654321
-987654321
987654321
-987654321
111333445
-112333445
8 rows selected.
SQL>
Ooops! Seems to be wrong, eh? But, if you fix the column format ...
SQL> col field1 format 999g999g999g990d000000
SQL> select * from test;
FIELD1
-----------------------
12.345,780000
-12.345,780000
987.654.321,000000
-987.654.321,000000
987.654.321,012340
-987.654.321,012340
111.333.444,887766
-112.333.444,887766
8 rows selected.
SQL>
See? Everything is here.
I guess that, if you used a GUI, you wouldn't have such problems. The former sentence doesn't mean that you should NOT use SQL*Plus - on the contrary; just ... learn how to use it properly :)
[EDIT, after reading the comment]
It is SET NUMFORMAT you might be looking for:
SQL> select -12334.789123 from dual;
-12334.789123
-------------
-12334,789
SQL> set numformat 999g999g990d000000
SQL> select -12334.789123 from dual;
-12334.789123
-------------------
-12.334,789123
SQL>