TO_DATE function is not working with timezone info. - sql

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.

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

date conversion in oracle for Fri Nov 23 2018 format

I want to convert "Fri Nov 23 2018" into a mm/dd/yyyy format in Oracle 12c, I tried but didn'tfind a proper solution, kindly help me on this
Convert your string to a DATE data type (note, a DATE is a binary data-type and has no defined format so it is just being used as an intermediate step) and then back to a string in the desired format:
SELECT TO_CHAR(
TO_DATE(
'Fri Nov 23 2018',
'Dy Mon DD YYYY',
'NLS_DATE_LANGUAGE=American'
),
'mm/dd/yyyy'
) AS formatted_date_string
FROM DUAL;
Which outputs:
FORMATTED_DATE_STRING
11/23/2018
db<>fiddle here

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.

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;