简体   繁体   English

Oracle 将带时区的 TIMESTAMP 转换为 DATE

[英]Oracle Convert TIMESTAMP with Timezone to DATE

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.+04:00 (Europe/Moscow)+04:00 (Europe/Moscow) ,需要将格式YYYY-MM-DD"T"HH24:MI:SSTZH:TZM的字符串转换为 Oracle 11g 中的DATE数据类型。

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) ).换句话说,我有一个字符串2013-11-08T10:11:31+02:00并且我想将它转换为DATE数据类型(在本地数据库时区+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 (ie with local timezone transformation of time to +04:00 (Europe/Moscow) ).对于字符串2013-11-08T10:11:31+02:00我想要的转换应该返回DATE数据类型,日期为2013-11-08 12:11:31 (即本地时区转换时间为+04:00 (Europe/Moscow) )。 Timezone of string may be different and +02:00 in string above is just example.字符串的时区可能不同,上面字符串中的+02:00只是示例。

I tried to do this with TIMESTAMP data type, but no success with time zone transformation.我尝试使用TIMESTAMP数据类型执行此操作,但时区转换没有成功。

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: at time zone子句的to_timestamp_tz()函数可用于将字符串文字转换timestamp with time zone数据类型的timestamp with time zone值:

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: 然后,我们使用cast()函数生成date数据类型的值:

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. 了解有关time zone子句和to_timestamp_tz()函数的更多信息。

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 将GMT中的时间戳转换为中欧时间的日期

if you want your timestamp with timezone to convert to a date in sync with "sysdate" then use the following: 如果您希望带有时区的时间戳转换为与“sysdate”同步的日期,请使用以下命令:

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

to cast time stamp to date : 到目前为止的时间戳:

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

WITH t1 (tm) AS (SELECT TIMESTAMP '2021-12-14 15:33:00 EET' FROM DUAL) 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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM