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.
In Oracle9i release 1 (9.0) and up -- yes. There is a datatype TIMESTAMP WITH TIMEZONE that does that. Before that -- no, the Oracle DATE datatype cannot store that, nor is it timezone aware.
Use the ALTER DATABASE SET TIME_ZONE command to change the time zone of a database. This command takes either a named region such as America/Los_Angeles or an absolute offset from UTC. This example sets the time zone to UTC: ALTER DATABASE SET TIME_ZONE = '+00:00';
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With