varchar2 string to Date format - sql

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.

Related

How to convert date : Sep 26 00:15:00 2020 in YYYY/MM/DD HH24:MI:SS' format in oracle sql

Error while converting
select to_date('Sep 26 00:15:00','DD/MM/YYYY HH24:MI:SS') from dual ;
ORA-01841: (full) year must be between -4713 and +9999, and not be 0 .
Looking for solutions
How? By providing correct format masks, of course.
SQL> select
2 to_char(
3 to_date('Sep 26 00:15:00 2020', 'Mon dd hh24:mi:ss yyyy'),
4 'yyyy/mm/dd hh24:mi:ss'
5 ) result
6 from dual;
RESULT
-------------------
2020/09/26 00:15:00
SQL>
With TO_DATE(), you want to convert a string - 'Sep 26 00:15:00' to an Oracle DATE type - that is a type that counts, internally, the number of seconds since an epoch date.
In Unix, that would be '1970-01-01 00:00:00', in some other databases I know '2000-01-01 00:00:00'. I don't know about Oracle.
So you convert the string: 'Sep 26 00:15:00'. That is: the three-letter English month name abbreviation, a space, the day as two digits, a space, two digits for the 24hour-notated hour, a colon, two digits for the minutes, another colon, and two digits for the second. Nothing for the year, to be precise. The second parameter of TO_DATE() must describe that format. It would therefore be: 'Mon DD HH24:MI:SS' .
The second parameter you use makes absolutely no sense in this context. You could, if you want to play, use it to re-format the converted DATE type, back to a string, in the ISO format, using TO_CHAR():
SELECT
TO_CHAR(
TO_DATE('Sep 26 00:15:00','Mon DD HH24:MI:SS')
, 'YYYY-MM-DD HH24:MI:SS'
)
FROM dual;

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.

Load File to Oracle TIMESTAMP(6) Format

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"
...