Load File to Oracle TIMESTAMP(6) Format - sql

I'm trying to load FLATFILE data to Oracle via Sqlloader, but I'm having trouble with a timestamp(6) field.
I have timestamp values in the below format:
Wed May 08 00:00:00 UTC 2013
I need sqlloader code to load this time format in oracle.
My current sqlloader code is below
LOAD DATA
INFILE 'MYDIR/my_documents_data.dat'
TRUNCATE
INTO TABLE sample_table
FIELDS TERMINATED BY '|'
TRAILING NULLCOLS
(
"MYID" INTEGER EXTERNAL,
"NAME" CHAR(100),
"DOCUMENTTYPE" CHAR(250),
"DATEADDED" TIMESTAMP,
"DOCUMENTSIZE" INTEGER EXTERNAL
)
Please advise me which format should I apply for this column "DATEADDED" TIMESTAMP, which is actually a timestamp(6).

Wed May 08 00:00:00 UTC 2013
Given that sample, the proper format is probably:
DY MON DD HH24:MI:SS TZR YYYY
See http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements004.htm#SQLRF00212 for the detail of the various fields.
You have to load your data as a timestamp with timezone though, because I used TZR to map the UTC part of your data. Something like that:
...
"DATEADDED" TIMESTAMP WITH TIME ZONE "DY MON DD HH24:MI:SS TZR YYYY"
...
If you really want a timestamp without timezone, use that format instead:
DY MON DD HH24:MI:SS "UTC" YYYY
This will assume that all your timestamps are UTC and will silently discard that information.
...
"DATEADDED" TIMESTAMP "DY MON DD HH24:MI:SS "UTC" YYYY"
...

Related

varchar2 string to Date format

I am trying to convert the below string value to TO_DATE but oracle is not
recognizing the date format. Where am I going wrong.
SELECT TO_DATE('Wed Oct 10 23:50:00 2018 UTC','Dy Mon DD HH24:MI:SS YYYY TZR','NLS_DATE_LANGUAGE = American')
FROM dual;
ERROR at line 1:
ORA-01821: date format not recognized
Use:
SELECT TO_TIMESTAMP_TZ('Wed Oct 10 23:50:00 2018 UTC','DY MON DD HH24:MI:SS YYYY TZR','NLS_DATE_LANGUAGE=American')
FROM dual;
TO_DATE function returns DATE datatype, which does not support timezones. You are using TZR format specifier in your query (time zone region), and this generates the error because DATE does not support this.
This documentation - Time Zones shows which datatypes supports timezones, and states that only TIMESTAMP WITH TIME ZONE and TIMESTAMP WITH LOCAL TIME ZONE do. So you must convert to one of this datatype instead of converting to DATE.
TO_TIMESTAMP_TZ function converts a literal to TIMESTAMP WITH TIME ZONE datatype.
If (and I realise it's a big 'if') the string values always contain UTC and not any other time zone values, then you could just treat that as a character literal. You would do that by changing your format model from TZR, which isn't recognised by to_date(), to "UTC" - including the double quotes:
SELECT TO_DATE('Wed Oct 10 23:50:00 2018 UTC',
'Dy Mon DD HH24:MI:SS YYYY "UTC"',
'NLS_DATE_LANGUAGE = American')
FROM dual;
TO_DATE('WEDOCT1023
-------------------
2018-10-10 23:50:00
Of course, as that is a plain date it still has no time zone information, but if you want to retain that then you need a timestamp with [local] time zone data type anyway.

Can't find good Date Format : impossible parsing

I'm trying to convert the following String to a datetime format:
Sat Jan 14 13:55:34 CET 2017
If we consider that my date is in the in_OutPut3 variable, I'm trying to use the following format pattern :
TO_DATE(in_OutPut3,'DY MON DD HH:MI:SS Z YYYY')
I receive the following error :
The system failed to parse the date format : 'DY MON DD HH:MI:SS Z
YYYY'.
Do you have any idea what is this date format ?
You have a timezone embedded in your string so try using the TO_TIMESTAMP_TZ function. TO_DATE is not designed to work with timezones.
The following works on 11g;
select to_timestamp_tz('Sat Jan 14 13:55:34 CET 2017','DY MON DD HH24:MI:SS TZR YYYY') from dual
This one worked for me:
to_timestamp('Sat Jan 14 13:55:34 CET 2017','Dy Mon dd hh:mi:ss ZZ yyyy');
Marco
You can just ignore the timezone while converting to Informatica Date type. Anyway, Informatica won't retain timezone information after converting to Data datatype. Put some symbols like underscore(_) to match 'CET'
TO_DATE(in_OutPut3, 'DY MON DD HH24:MI:SS ___ YYYY')
Also, looks like you have the hours in military format. In that case you have to use HH24.

Convert a date/time from UTC to session time zone

My program receives timestamps in UTC, e.g. Tue, 31 May 2016 11:43:47 UTC. How do I convert them to a timestamp in the session's current timezone?
First convert the string to a timestamp with timezone (use the correct function). To show the result in your local (session) time zone, use the "AT LOCAL" clause.
select to_timestamp_tz('Tue, 31 May 2016 19:43:47 UTC',
'Dy, dd Mon yyyy hh24:mi:ss tzr') at local from dual;
Result (using my front-end settings for displaying timestamp with timezone - yours may be different):
31-MAY-16 02.43.47.000000000 PM AMERICA/CHICAGO
Use the tz_offset function to get the timezone offset for the session's timezone, then use the from_tz function to convert the string to a timestamp with time zone, and use at time zone to change it to use the session's timezone.
select from_tz(to_timestamp('Tue, 31 May 2016 11:43:47 UTC'
,'Dy, dd Mon yyyy hh24:mi:ss "UTC"')
, 'UTC')
at time zone tz_offset(sessiontimezone)
,sessiontimezone
from dual;
31/05/2016 19:43:47.000000000 +08:00 Australia/Perth

Cannot convert PSQL string to timestamp

I am trying to do a simple string to date conversion; however, PSQL complains when there is a timezone in that string. Their documentation clearly states that its supported; however, it complains. I don't even care about the timezone, I just want to convert the string.
db=> select to_timestamp('Mon Feb 23 13:43:07 PST 2015', 'Dy Mon DD HH24:MI:SS TZ YYYY')::timestamp without time zone;
ERROR: "TZ"/"tz" format patterns are not supported in to_date
Postgres version: 9.3.10
http://www.postgresql.org/docs/9.3/static/functions-formatting.html
Why not try a straight cast?
SELECT 'Mon Feb 23 13:43:07 PST 2015'::timestamp with time zone
Once you've done this it's pretty easy to work with the date object.

convert date in sql using to_char function

I would like to convert the following date to this format(DD-MON-YYYY).
I tried executing the below query but I got the error saying "date format not recognised".
select to_char(to_date('Sat Dec 01 00:00:00 IST 2012','EEE Mon dd HH:mm:ss z yyyy'),'DD-MON-YYYY') from dual;
For everything but the time zone:
'DY MON DD HH24:MI:SS YYYY'
For timezone support you need to use a conversion function that supports a timezone like TO_TIMESTAMP_TZ() and have the time zone name as one Oracle recognizes in the form it recognizes.
select to_char( TO_TIMESTAMP_TZ(
REPLACE( 'Sat Dec 01 21:00:00 IST 2012','IST','Asia/Calcutta'),
'DY MON DD HH24:MI:SS TZR YYYY'),'DD-MON-YYYY') from dual;
The relation between time zone names and abbreviations is one to many for most.
SELECT tzname, tzabbrev FROM v$timezone_names where TZABBREV = 'IST'
For your example it would probably be easier to remove some or all of the date parts you don't need in the output before conversion.
select to_char( to_date( replace('Sat Dec 01 21:00:00 IST 2012','IST',''),
'DY MON DD HH24:MI:SS YYYY'),'DD-MON-YYYY') from dual;