2016-03-21 33 views
0

我的下面的查询将当前时间增加了四天,但我需要向它添加四个小时。我正在添加UTC偏移量。将从子查询派生的小时添加到我的时间戳

select ,REQUEST_TIME, 
to_char(timestamp((REQUEST_TIME - cast(select extract(hours from 
(select TIMEZONE(current_timestamp, 'America/New_York','Etc/GMT')) - current_timestamp)))) , 
'YYYY-MM-DD HH24') as request_time_EST from Table 
+1

欢迎来到Stack Overflow! 。请编辑您的文章并进行格式化以提高可读性。 – gmuraleekrishna

回答

0

你正在运行到这里的问题是,小时提取物的结果(见下文B1)返回一个整数,再减去从REQUEST_TIME被减去给人一种结果是天数你没有想到或想要(见下面的C2)。

我相信,你所要找的东西需要将B1值转换为几小时(见下面的D1)。

选择REQUEST_TIME, TIMEZONE(CURRENT_TIMESTAMP, '美国/纽约', 'ETC/GMT') -

current_timestamp A1, 
extract(hours from TIMEZONE(current_timestamp, 'America/New_York','Etc/GMT') - current_timestamp) B1, 
request_time - extract(hours from TIMEZONE(current_timestamp, 'America/New_York','Etc/GMT') - current_timestamp) C1, 
request_time - cast(extract(hours from TIMEZONE(current_timestamp, 'America/New_York','Etc/GMT') - current_timestamp) || ' hours ' as interval) D1 
from table1; 

TESTDB.ADMIN(ADMIN)-> from table1; 
    REQUEST_TIME  | A1 | B1 |  C1  |   D1   
---------------------+----------+----+------------+--------------------- 
2016-01-31 12:00:00 | 04:00:00 | 4 | 2016-01-27 | 2016-01-31 08:00:00 
(1 row) 

你提到你的问题,并在你的示例代码使用子查询,我但我没有”在这种情况下,不需要再次选择。

+0

嗨斯科特,我在我的表中有一个字段作为UTC格式的REQUEST_TIME。我需要通过减去4或5来动态地将其转换为纽约时间,具体取决于光线节省情况。此查询为我提供当前的偏移量(4):select extract(从 (选择TIMEZONE(current_timestamp,'America/New_York','Etc/GMT')) - current_timestamp .....中的小时数.....我需要添加此偏移量到REQUEST_TIME字段如果我straightawy做减法它减去4天,但我想减去4小时我知道4间隔的演员命令,但我怎么能通过这个派生的价值我的查询 –

+0

好吧,我完全误解了你的问题,但我想我现在得到你想要的东西。报废/重做我的答案。 – ScottMcG