2016-03-21 19 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