以下查询选择unix时间戳。它应该是下午1点,但因为UTC而说是下午5点。它需要在东部时间下午1点,所以我需要从它减去4个小时。什么是最好的方法呢?
SELECT CAST(to_date('1970-01-01', 'YYYY-MM-DD') + substr(STARTTIME,0,10)/60/60/24 as timestamp)
答案 0 :(得分:1)
你不应该手动计算。您应该添加或减去时区。
Oracle how to convert time in UTC to the local time (offset information is missing)
这是一个示例查询:
SELECT TO_CHAR (
FROM_TZ (CAST (SYSDATE AS TIMESTAMP), 'UTC')
AT TIME ZONE 'EUROPE/BERLIN',
'YYYY-MM-DD HH24:MI:SS TZH:TZM TZR')
AS BERLIN_Time_complete,
TO_CHAR (
FROM_TZ (CAST (SYSDATE AS TIMESTAMP), 'UTC')
AT TIME ZONE 'UTC',
'YYYY-MM-DD HH24:MI:SS TZH:TZM TZR')
AS UTC_Complete,
TO_CHAR (
FROM_TZ (CAST (SYSDATE AS TIMESTAMP), 'UTC')
AT TIME ZONE 'EUROPE/BERLIN',
'YYYY-MM-DD HH24:MI:SS TZH:TZM')
AS BERLIN_Time_complete,
TO_CHAR (
FROM_TZ (CAST (SYSDATE AS TIMESTAMP), 'UTC')
AT TIME ZONE 'EUROPE/BERLIN',
'TZH:TZM')
AS BERLIN_Timezone,
TO_CHAR (
FROM_TZ (CAST (SYSDATE AS TIMESTAMP), 'UTC')
AT TIME ZONE 'EUROPE/BERLIN',
'TZR')
AS Timezone_Name
FROM DUAL;
关键是格式:
您应该使用这些,以了解to_date()
/ to_char()
。你需要它。
如果你真的想增加几个小时。这是一个例子:
select sysdate + INTERVAL '2' HOUR from dual;
答案 1 :(得分:-1)
你可以试试这个:
SELECT TO_CHAR(sysdate, 'DD-MON-YYYY HH24:MI:SS') AS myDate
FROM dual;
有关详细信息,请参阅this document。