Oracle ctl file configurations for converting date time to date - oracle

I have a ctl file for inserting data to oracle from a txt file. The code is:
OPTIONS (SKIP=1)
LOAD DATA
APPEND
INTO TABLE TIME_TRACK_MY_TRAN
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
VC_EMP_ID,
VC_NAME,
NU_ENTERED_FROM,
DT_DATE,
NU_FLAG,
DT_REAL_DATE "TO_DATE(SUBSTR(TO_CHAR(:DT_REAL_DATE), 0, 9), 'dd/mm/yyyy')",
DT_DATE_TIME "to_char(to_date(:'DT_DATE_TIME','yyyy-mm-dd hh24:mi:ss'),'HH24miss')",
CH_IN_OUT_STATUS,
SR_NO "time_track_my_tran_seq.nextval",
date_time "TO_DATE(SUBSTR(TO_CHAR(:DT_REAL_DATE), 0, 9), 'dd/mm/yyyy')"
The code works well for short dates like 1/9/2020 and 2/3/2020 However I discovered that for longer dates like 10/10/2020 and 22/10/2020 it misbehaves and inserts the year wrongly like instead of 2020 it inserts 0202. I tried changing the range from 0,9 to 0,10 then 1,10 but it gives me an error that:
SQL*Loader: Release 10.2.0.1.0 - Production on Thu Oct 22 16:31:53 2020
Copyright (c) 1982, 2005, Oracle. All rights reserved.
SQL*Loader-350: Syntax error at line 17.
Expecting "," or ")", found end of file.

Format for sqlloader ctl-file is different to SQL, I think it should be similar to this one:
VC_EMP_ID,
VC_NAME,
NU_ENTERED_FROM,
DT_DATE,
NU_FLAG,
DT_DATE_DATE DATE 'yyyy-mm-dd hh24:mi:ss', # don't split data and time component to different columns
CH_IN_OUT_STATUS,
SR_NO "time_track_my_tran_seq.nextval",
date_time DATE 'dd/mm/yyyy'

Related

Oracle SQL*Loader WHEN Clause Raising Error 2

I am trying to exclude the last line of a data file using SQL*Loader, using the WHEN clause, but when it gets to that line it populates both the bad and discard file, and raises an Error 2.
The line to ignore is the last line and starts with "NOL".
After some reading, Error 2 is a warning about the synatx of the CTL file, but cannot find out where it is wrong. Note, if l remove the last line and then run the SAME CTL file, no ERROR is raised, so the issue cannot be the synatx of the CTL file.
To resolve the issue, l am removing the last line BEFORE loading the data, but would like to find out what the issue is for any future use of the WHEN clause.
I have tried:
file_dt != 'NOL'
(1:1) != 'N'
. . .
But l get the same Error 2.
Has anybody else come across this issue? Or have something that l can try?
Oracle Docs
SQL*Loader Command-Line Reference
For UNIX, the exit codes are as follows:
EX_SUCC 0
EX_FAIL 1
EX_WARN 2
EX_FTL 3
Data:
File-Date,Number
2021-05-04,24
2021-05-04,24
2021-05-04,24
2021-05-04,24
NOL: 4
CTL File:
OPTIONS (READSIZE=51200001, BINDSIZE=51200000, ROWS=5000, ERRORS=0, SKIP=1)
load data
append
into table SOME_SCHEMA.SOME_TABLE
WHEN (01) 'NOL'
fields terminated by ';'
OPTIONALLY ENCLOSED BY '"' AND '"'
trailing nullcols
(
file_dt DATE "YYYY-MM-DD",
a_number
)
Result:
Path used: Conventional
Commit point reached - logical record count 5
Table SOME_SCHEMA.SOME_TABLE:
4 Rows successfully loaded.
Check the log file:
loading-file.log
for more information about the load.
2021-05-06 09:42:12: Finished Loading Data into Table
2021-05-06 09:42:12: Status: 2
I'm not on Unix. Nonetheless, loading should work the same.
Table:
SQL> desc test
Name Null? Type
----------------------------------------------------- -------- --------------------------
FILE_DT DATE
A_NUMBER NUMBER
SQL>
Control file (I included sample data into it, for simplicity):
OPTIONS (READSIZE=51200001, BINDSIZE=51200000, ROWS=5000, ERRORS=0, SKIP=1)
load data
infile *
replace
into table test
WHEN (01) <> 'NOL'
fields terminated by ','
trailing nullcols
(
file_dt DATE "YYYY-MM-DD",
a_number
)
begindata
File-Date,Number
2021-05-04,24
2021-05-04,24
2021-05-04,24
2021-05-04,24
NOL: 4
Loading session and result:
SQL> $sqlldr scott/tiger#orcl control=test38.ctl log=test38.log
SQL*Loader: Release 11.2.0.1.0 - Production on ╚et Svi 6 11:38:00 2021
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Commit point reached - logical record count 4
SQL> select * from test;
FILE_DT A_NUMBER
------------------- ----------
04.05.2021 00:00:00 24
04.05.2021 00:00:00 24
04.05.2021 00:00:00 24
04.05.2021 00:00:00 24
Seems to be OK.
So, what did I do differently?
modified WHEN clause
fields are terminated by comma, not semi-colon
removed superfluous information

Failed to parse input string "31-Dec-2019" ERROR

Data Type of my date column in the big query table is String.
Format of the date: 31-Dec-2019
I have records for 2018, 2019, 2020 years.
Now I want to filter out the data after 2020-01-01.
With the below code, where-condition is not working here and returning all the records of the table.
Select
*
from table T
where date > '2020-01-01'
Tried the below and their respective errors:
PARSE_DATE('%d-%m-%Y',SUBSTR(date,1,12)) - Failed to parse input string "31-Dec-2019"
CAST( date as DATE) > '2020-01-01' -- returning all the records in the table/where condition is not working
Can someone please help me with this?
You need to use %b for abreviated month name.
select PARSE_DATE('%d-%b-%Y',SUBSTR('31-Dec-2019',1,12))

why the date info of minute was wrong after conversion

I used SQL to convert a date:
select date,to_char(date,'yyyy/mm/dd HH24:mm') from process
The original date is 12/5/2018 2:41:06 PM
but the conversion result is 2018/12/05 14:12.
Is my SQL wrong?
mm is the placeholder for the month - regardless of the position. So the second mm contains the month again.
As documented in the manual the placeholder for minutes is mi
So you need: to_char(date,'yyyy/mm/dd HH24:MI')

Single Month Digit Date Format issue in Oracle

Am getting the below issue when am using 'mon-d-yyyy' to convert date to char, as i need a single day digit for values from 1 to 9 days in a month.
When i use the 'mon-d-yyyy' format, am losing out on 5 days and getting a wrong date. Any help on this would be great.
select to_char(sysdate-22,'mon-d-yyyy') from dual;--aug-2-2017
select to_char(sysdate-22,'mon-dd-yyyy') from dual;--aug-07-2017
select sysdate-22 from dual;--07-AUG-17 11.06.43
In Oracle date formats, d gets the day of week. The 2 in your output means monday, not august the 2nd.
Try using Fill Mode as Format Model Modifier
select to_char(sysdate-22,'mon-fmdd-yyyy') from dual;
One option might be to piece together the date output you want:
SELECT
TO_CHAR(sysdate-22, 'mon-') ||
TRIM(LEADING '0' FROM TO_CHAR(sysdate-22, 'dd-')) ||
TO_CHAR(sysdate-22, 'yyyy')
FROM dual;
The middle term involving TRIM strips off the leading zeroes, if present, from the date.
Output:
Demo here:
Rextester
SQL>SELECT TO_CHAR(TO_DATE('29-AUG-2017','DD-MON-YYYY') - 22,'"WEEKDAY :"D, MON-FMDD-YYYY') "Before22Days" FROM DUAL;
D- Gives you a numeric weekday(2nd weekday in a week) on AUG-07-2017.
DD-Gives a Numeric Month Day i.e,07th
FMDD-Gives 7th
Before22Days
----------------------
WEEKDAY :2, AUG-7-2017

oracle sqlldr time format

I'm using oracle sqlldr (for bulk load operations), but I can't convert this datetime format (first column):
File contents:
Jan 1 1900 11:36:56:000PM|968|409|198|33|30|45|19
Jan 1 1900 11:36:57:000PM|967|415|198|34|33|43|21
Jan 1 1900 11:36:59:000PM|966|427|197|34|33|40|19
Control file contents:
load data
infile '/home/bim/oraload/data/AERO.SONDAJ.samsun.txt'
append
into table AERO.SONDAJ
fields terminated by "|"
TRAILING NULLCOLS
(
refsaat date 'MON DD YYYY HH24:mi:ss', --not running
bsnsvy,
yuks,
sck,
nem,
isba,
rzgyon,
rzghiz
)
Try something like this. Inorder for this to work, the refsaat type should be a timestamp type and not DATE data type. Date Data type does not store beyond seconds.
refsaat TIMESTAMP 'Mon DD YYYY HH:mi:ss:ff3PM'

Resources