根据时区将TIMESTAMP转换为DATE

时间:2018-12-06 13:10:04

标签: oracle timezone

我正在检查DATE如何存储在数据库中。考虑以下语句:

SELECT
CAST (to_timestamp_tz('2018-12-05T10:00:00+01:00', 'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM') AS DATE) AS PRAGUE_TIME,
CAST (to_timestamp_tz('2018-12-05T10:00:00+00:00', 'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM') AS DATE) AS GMT_TIME
FROM DUAL

结果:

PRAGUE_TIME          GMT_TIME
2018-12-05 10:00:00  2018-12-05 10:00:00

尽管是从+1时区偏移创建的,但时间是相同的。可以肯定的是,我向字符串添加了进一步的转换:

SELECT
TO_CHAR(CAST (to_timestamp_tz('2018-12-05T10:00:00+01:00', 'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM') AS DATE),'YYYY-MM-DD HH24:MI:SS') AS PRAGUE_TIME,
TO_CHAR(CAST (to_timestamp_tz('2018-12-05T10:00:00+00:00', 'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM') AS DATE),'YYYY-MM-DD HH24:MI:SS') AS GMT_TIME
FROM DUAL


PRAGUE_TIME          GMT_TIME
2018-12-05 10:00:00  2018-12-05 10:00:00

相同的结果。那么如何在不丢失时区信息的情况下将TIMESTAMP转换为DATE

1 个答案:

答案 0 :(得分:3)

  

如何在不丢失时区信息的情况下将TIMESTAMP转换为DATE

你不能。

  • DATE仅包含年,月,日,时,分和秒。
  • TIMESTAMP具有所有这些成分,加上纳秒级,还可以包含时区或时区偏移量成分。

如果您将TIMESTAMP转换为DATE,则会丢失DATE无法存储的信息。

您可以做的是使用TIMESTAMP将所有AT TIME ZONE 'UTC'转换为相同的时区:

SELECT CAST( TIMESTAMP '2018-12-05 10:00:00+01:00' AT TIME ZONE 'UTC' AS DATE )
         AS PRAGUE_TIME_AS_UTC,
       CAST( TIMESTAMP '2018-12-05 10:00:00+00:00' AT TIME ZONE 'UTC' AS DATE )
         AS GMT_TIME_AS_UTC
FROM   DUAL

结果

|   PRAGUE_TIME_AS_UTC |      GMT_TIME_AS_UTC |
|----------------------|----------------------|
| 2018-12-05T09:00:00Z | 2018-12-05T10:00:00Z |