Can't find good Date Format : impossible parsing - sql

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.

Related

How to convert long verbal datetime to timestamp(YYYY-MM-DD HH:MM:SS) in SNOWFLAKE

In snowflake, I have a bad date format, something like this -
'Tue Aug 06 18:22:59 EDT 2019'
I am trying to convert it to the following date format -
YYYY-MM-DD HH:MM:SS
I have tried various TO_TIMESTAMP versions but nothing seems to work. Any suggestions/ help is truly appreciated.
SELECT TO_TIMESTAMP_NTZ('Wed May 29 23:36:39 EDT 2019', 'DY MON DD HH:MM:SS YYYY')
Error msg - Can't parse 'Wed May 29 23:36:39 EDT 2019' as timestamp with format'DY MON DD HH:MM:SS YYYY'
This works in my Snowflake environment:
SELECT to_timestamp('Wed May 29 23:36:39 EDT 2019', 'DY MON DD HH24:MI:SS EDT YYYY') as dt
Using MI for minutes (not MM).
Output: 2019-05-29 23:36:39.000

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.

TO_DATE function is not working with timezone info.

I am trying below :
SELECT TO_CHAR( TO_DATE('Wed May 18 00:00:00 IST 2016', 'DY MON DD HH24:MI:SS TZR YYYY'),'DD-MM-YYYY') FROM DUAL;
and expecting 18-05-2016
But it says - 01821. 00000 - "date format not recognized"
It works well if I remove IST and corresponding TZR.
What's going wrong ?
The Oracle DATE data type does not have a time zone component. Neither does a plain TIMESTAMP. You need to convert to a TIMESTEMP WITH TIME ZONE:
SELECT TO_CHAR(TO_TIMESTAMP_TZ('Wed May 18 00:00:00 IST 2016',
'DY MON DD HH24:MI:SS TZR YYYY', 'NLS_DATE_LANGUAGE=ENGLISH'),
'DD-MM-YYYY')
FROM DUAL;
But IST is not a recognised time zone region, so that will still get ORA-01882: timezone region not found. You may have to explicitly replace that with a known region, e.g.:
SELECT TO_CHAR(TO_TIMESTAMP_TZ(REPLACE('Wed May 18 00:00:00 IST 2016',
'IST', 'Asia/Calcutta'), 'DY MON DD HH24:MI:SS TZR YYYY', 'NLS_DATE_LANGUAGE=ENGLISH'),
'DD-MM-YYYY')
FROM DUAL;
TO_CHAR(TO
----------
18-05-2016
Alternatively, if you won't be doing any manipulation on it and want the IST year anyway, you can extract the date components directly from the string, or treat IST as a fixed value and not try to interpret it at all:
SELECT TO_CHAR(TO_DATE('Wed May 18 00:00:00 IST 2016',
'DY MON DD HH24:MI:SS "IST" YYYY', 'NLS_DATE_LANGUAGE=ENGLISH'),
'DD-MM-YYYY')
FROM DUAL;
TO_CHAR(TO
----------
18-05-2016
... but that may not be suitable for your actual situation.

TO_DATE function time zone parse error

I have a problem when trying to parse a date in Oracle:
TO_DATE('Fri May 16 14:30:57 EDT 2014', 'DY MON DD HH24:MI:SS TZD YYYY')
Error:
ORA-01821: date format not recognized
What is wrong with the timezone?
This one works:
select TO_TIMESTAMP_TZ('May 16 14:30;57 EDT 2014', 'MON DD HH24:MI:SS TZD YYYY')
from dual;

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;