14

I have DB with timezone +04:00 (Europe/Moscow) and need to convert a string in format YYYY-MM-DD"T"HH24:MI:SSTZH:TZM to DATE data type in Oracle 11g.

In other words, I have a string 2013-11-08T10:11:31+02:00 and I want to convert it to DATE data type (in local DB timezone +04:00 (Europe/Moscow)).

For string 2013-11-08T10:11:31+02:00 my desired transformation should return DATE data type with date 2013-11-08 12:11:31 (i.e. with local timezone transformation of time to +04:00 (Europe/Moscow)). Timezone of string may be different and +02:00 in string above is just example.

I tried to do this with TIMESTAMP data type, but no success with time zone transformation.

3
  • Except for situations that need 'local' times (ie, scheduling/planning, especially for timezones with daylight savings), it's usually best to store absolute timestamps stored in UTC (and translate on display/report). What are these timestamps being used for? Nov 20, 2013 at 11:53
  • I receive date in this timestamp format from external system and do not know why external system uses them. :( Nov 21, 2013 at 12:37
  • Found my answer.. but in case if you want to double check that timestamp is true or false, I found a good site for this. onlinetimestampconvert.com Dec 19, 2014 at 17:29

5 Answers 5

17

to_timestamp_tz() function with at time zone clause can be used to convert your string literal to a value of timestamp with time zone data type:

SQL> with t1(tm) as(
  2    select '2013-11-08T10:11:31+02:00' from dual
  3  )
  4  select to_timestamp_tz(tm, 'yyyy-mm-dd"T"hh24:mi:ss TZH:TZM')
  5           at time zone '+4:00'         as this_way
  6       , to_timestamp_tz(tm, 'yyyy-mm-dd"T"hh24:mi:ss TZH:TZM')
  7           at time zone 'Europe/Moscow' as or_this_way
  8    from t1
  9  /

Result:

THIS_WAY                            OR_THIS_WAY
----------------------------------------------------------------------------
2013-11-08 12.11.31 PM +04:00       2013-11-08 12.11.31 PM EUROPE/MOSCOW

And then, we use cast() function to produce a value of date data type:

with t1(tm) as(
  select '2013-11-08T10:11:31+02:00' from dual
)
select cast(to_timestamp_tz(tm, 'yyyy-mm-dd"T"hh24:mi:ss TZH:TZM') 
         at time zone '+4:00' as date)   as this_way  
     , cast(to_timestamp_tz(tm, 'yyyy-mm-dd"T"hh24:mi:ss TZH:TZM') 
         at time zone 'Europe/Moscow' as date) as or_this_way
  from t1

This_Way             Or_This_Way 
------------------------------------------
2013-11-08 12:11:31  2013-11-08 12:11:31 

Find out more about at time zone clause and to_timestamp_tz() function.

1
  • 2
    Thank you! select CAST(to_timestamp_tz('2013-11-08T10:11:31+02:00', 'yyyy-mm-dd"T"hh24:mi:ss TZH:TZM') at time zone DBTIMEZONE AS DATE) from dual will be correct way for me Nov 20, 2013 at 8:28
13
SELECT
CAST((FROM_TZ(CAST(timezonefield AS TIMESTAMP),'GMT') AT TIME ZONE 'CET') AS DATE) 
FROM table;

Converts a timestamp in GMT to date in Central European time

0
2

if you want your timestamp with timezone to convert to a date in sync with "sysdate" then use the following:

select cast(to_timestamp_tz('2013-11-08T10:11:31-02:00'
                           ,'yyyy-mm-dd"T"hh24:mi:sstzh:tzm'
                           ) 
            at time zone to_char(systimestamp
                                ,'tzh:tzm'
                                ) 
            as date
           )
from dual
1
  • I cannot tell you how long I searched for some way to do this!
    – Gerrat
    Aug 13, 2021 at 14:12
0

to cast time stamp to date :

cast(registrationmaster.Stamp5DateTime as date) >= '05-05-2018' AND cast(registrationmaster.Stamp5DateTime as date) <= '05-05-2018'

0

you can manage timezones with CAST(x AT TIME ZONE 'YYYY' AS DATE), this helps me:

WITH t1 (tm) AS (SELECT TIMESTAMP '2021-12-14 15:33:00 EET' FROM DUAL)

SELECT 'EET' tz, CAST (tm AT TIME ZONE 'Europe/Kaliningrad' AS DATE) AS datetime FROM t1
union SELECT 'MSK' tz, CAST (tm AT TIME ZONE 'Europe/Moscow' AS DATE) AS datetime FROM t1
union SELECT 'CET' tz, CAST (tm AT TIME ZONE 'Europe/Prague' AS DATE) AS datetime FROM t1
union SELECT 'UTC' tz, CAST (tm AT TIME ZONE 'UTC' AS DATE) AS datetime FROM t1
1
  • Welcome to Stack Overflow. Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See [How to Answer]stackoverflow.com/questions/how-to-answer) Dec 14, 2021 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.