Oracle sqlldr timestamp format issue - oracle

I'm having an issue getting sqlldr to import a DAT file data file into my table, specifically with the field that is a timestamp.
2018-11-02T20:54:38.000000+0000
I've tried all manner of combinations in my control file and am going around in circles. Does anyone know what should be using in my control file for this above timestamp format?
For reference, this is what I've most recently tried:
load data
infile 'feed.dat'
into table cust_acct
fields terminated by "|"
( ...
updateddatetime TIMESTAMP "YYYY-MM-DD-HH24.MI.SS",
...)

The date 2018-11-02T20:54:38.000000+0000 has a time-zone component so you want TIMESTAMP WITH TIME ZONE data type and you have 6 fractional seconds digits so your data type should have precision of 6.
In a DateTime format model you can use double quotes to indicate a literal string and, in sqlldr you can escape the double quotes with a backslash:
updateddatetime TIMESTAMP(6) WITH TIME ZONE "YYYY-MM-DD\"T\"HH24:MI:SS.FF6TZR",
or
updateddatetime TIMESTAMP(6) WITH TIME ZONE "YYYY-MM-DD\"T\"HH24:MI:SS.FF6TZHTZM",

Related

Reformat date in SQL Loader

I am loading Oracle tables using SQL Loader and I have an issue with date formats. The CSV files with the data contain strings in the format YYYY-MM-DD HH:MM:SS, but the Oracle tables require date format of DD-MON-YY.
I am currently going through the CSV files line by line to look for and reformat any dates before the load, but the files can reach 10M+ rows and this can be a pretty slow process. Does SQL Loader allow date reformatting in the load?
I'm looking for something like
LOAD DATA
INFILE 'data.csv'
TRUNCATE
INTO TABLE data
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(
COL1,
COL2,
CREATED_DATE DATE 'DD-MON-YY',
LAST_UPDT_DATE DATE 'DD-MON-YY
)
I've read suggestions that this DATE command can format dates automatically but has given no luck so far.
Assuming created_date and last_updt_date are actually defined as date in the database, they don't have a format. They're stored in an internal packed binary format that is not human readable.
Your control file needs to specify the format of the strings in the flat file that represent the date. You say the format is "YYYY-MM-DD HH:MM:SS" but that doesn't make sense. Minutes are MI not MM so I assume that is a type. HH is a 12 hour time format but that doesn't make sense without an AM/PM indicator. So I'm guessing your strings are using a 24 hour time format which is HH24. So my guess is that you want
CREATED_DATE DATE 'YYYY-MM-DD HH24:MI:SS',

Storing timestamp values stored in a varchar2 column into a date column in oracle

I have a column in a table that stores timestamp values as
"2018-01-12 16:13:51.107000000", i need to insert this column into a date column in another table, what format mask do i have to use here..
I have used the mask 'YYYY-MM-DD HH24:MI:SS.FF' but shows 'date format not recognized'.
I am assuming that you were trying to use TO_DATE on your text timestamp data. This won't work, because Oracle dates do not store anything more precise than seconds. Since your timestamps have fractional seconds, you may use TO_TIMESTAMP here, then cast that actual timestamp to a date:
SELECT
CAST(TO_TIMESTAMP('2018-01-12 16:13:51.100000',
'YYYY-MM-DD HH24:MI:SS.FF') AS DATE)
FROM dual;
12.01.2018 16:13:51
Demo
You can do this with a single call to TO_DATE(), but you must give the correct format model. Note that this solution is simpler (and possibly faster - if that matters) than converting to a timestamp and then casting to date.
If you want TO_DATE() to ignore part of the input string, you can use the "boilerplate text" syntax in the format model. That is enclosed in double quotes. For example, if your string included the letter T somewhere and it had to be ignored, you would include "T" in the same position in the format model.
This has some flexibility. In your case, you must ignore the decimal point, and up to nine decimal digits (the maximum for timestamp in Oracle). The format model will allow you to use ".999999999" (or any other digits, but 9999... is used by most programmers) to ignore a decimal point and UP TO nine digits after that.
Demo: (notice the double-quoted "boilerplate text" in the format model)
select to_date('2018-01-12 16:13:51.100000',
'YYYY-MM-DD HH24:MI:SS".999999999"') as dt
from dual;
DT
-------------------
2018-01-12 16:13:51

How to load files using SQLLDR with date format as yyyymmddhhmmss?

I need to load a table with a .csv file which contains date "20140825145416".
I have tried using (DT date "yyyymmdd hh24:mm:ss") in my control file.
It throws an error as ORA-01821: date format not recognized
I require the data in table as "MM/DD/YYYY HH:MM:SS".
Sample data : 20140825145416
thanks in advance.
Well, I would be remiss if I did not point out that the correct answer is to never store dates as VARCHAR2 data, but make it a proper DATE column and load it like this:
DT DATE "YYYYMMDDHH24MISS"
Formatting is done when selecting. It will make your life so much easier if you ever need to use that date in a calculation.
That out of the way, If you have no control over the database and have to store it as a VARCHAR2, first convert to a date, then use to_char to format it before inserting:
DT CHAR "to_char(to_date(:DT, 'YYYYMMDDHH24MISS'), 'MM/DD/YYYY HH24:MI:SS')"
Note 'MI' is used for minutes. You had a typo where you used 'MM' (months) again for minutes.
I know it's already been said in the previous answer, but it's so important, it's worth repeating. Do not store dates as varchars !!
If your DT column is timestamp then this might work
DT CHAR(25) date_format TIMESTAMP mask "yyyymmddhhmiss"
I used something like this in external tables. Maybe this might help
https://docs.oracle.com/cd/B19306_01/server.102/b14215/et_concepts.htm
and
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:8128892010789

how to insert iso-8601 type date into oracle database

I am using Oracle 11g and trying to figure out how to insert this date into my table. The date seems like it is ISO-8601, but the 7 zeros are confusing me.
Insert into myTestTable (myDate) values ('2013-01-22T00:00:00.0000000-05:00');
I have tried to format the date with no luck. The error I am getting is ORA-01861 literal does not match format string.
First of all, the column must be a TIMESTAMP WITH TIME ZONE type to hold the value you're trying to insert. An Oracle DATE is accurate to seconds only, and it doesn't hold a time zone.
The seven zeros are the fractional seconds. The default precision for a TIMESTAMP WITH TIME ZONE happens to be seven decimal places. To specify three decimal places for seconds, define the column as TIMESTAMP(3) WITH LOCAL TIME ZONE.
The actual number of decimal places returned by SYSTIMESTAMP, which is the current timestamp at the server, depends on the operating system. My local Windows 7 Oracle returns three significant decimal places, while the Solaris OS at one of my clients returns six significant decimal places.
As for inserting the value, if you do something like this...
insert into myTestTable (myTS) values ('2013-01-22T00:00:00.0000000-05:00');
... Oracle will try to convert the timestamp using its current NLS_TIMESTAMP_TZ_FORMAT setting. You can query the setting like this:
SELECT *
FROM NLS_Session_Parameters
WHERE Parameter = 'NLS_TIMESTAMP_TZ_FORMAT';
PARAMETER VALUE
----------------------- ----------------------------
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
The result I got is the "factory default". Yours is probably the same, and you can see it doesn't match the format you've given, hence the conversion fails.
As another answer correctly notes, you can use the TO_TIMESTAMP_TZ function and a format string to convert the string to a timestamp. You can also use an ANSI timestamp literal:
insert into myTestTable (myTS)
values (TIMESTAMP '2013-01-22T00:00:00.0000000-05:00');
Oracle documents timestamp literals here. The link covers literals for all types, so you'll need to scroll about two-thirds of the way down the screen (or do a find) to get to the timestamp literals.
Here is the answer.
insert into myTestTable (myDate) values
(to_timestamp_tz('2013-01-22T00:00:00.0000000-05:00',
'YYYY-MM-DD"t"HH24:MI:SS.FF7TZR'));

Oracle sqlldr timestamp format headache

I'm struggling to get sqlldr to import a csv data file into my table, specifically with the field that is a timestamp.
The data in my csv file is in this format:
16-NOV-09 01.57.48.001000 PM
I've tried all manner of combinations in my control file and am going around in circles. I can't find anything online - not even the Oracle reference page that details what all the date/timestamp format strings are.
Does anyone know where this reference page is, or what format string I should be using in my control file for this timestamp format.
For reference, this is what I've most recently tried:
load data
infile 'kev.csv'
into table page_hits
fields terminated by "~"
( ...
event_timestamp TIMESTAMP "dd-mmm-yy hh24.mi.ss",
...)
you can try this format:
event_timestamp TIMESTAMP "dd-MON-yy hh.mi.ss.ff6 PM"
You can browse all available formats in the SQL reference documentation.

Resources