2016-02-24 93 views
1

我正在運行PostgreSQL 9.4,並在我的查詢中正確地轉換時區。更新數據庫的服務器是XMPP聊天服務器,所涉及的列是sent_date,它的數據類型爲timestampzPostgreSQL時區轉換

首先,我測試的轉換從UTC工程,以東部時間靜態(第一無需轉換,然後用):

tcmadb-pub=> select '2016-02-24 05:52:58.686893'::timestamp with time zone; 
      timestamptz 
------------------------------- 
2016-02-24 05:52:58.686893+00 
(1 row) 

tcmadb-pub=> select '2016-02-24 05:52:58.686893'::timestamp with time zone AT TIME ZONE 'AEDT'; 
      timezone 
---------------------------- 
2016-02-24 16:52:58.686893 
(1 row) 

然而,當我運行實際的查詢,我只看到數據timestampz和不如時區時間:

SELECT sent_date 
FROM jm 
WHERE 
    from_jid like '[email protected]%' AND 
    sent_date::timestamp with time zone AT TIME ZONE 'AEDT' BETWEEN '2016-02-23' AND '2016-02-29'; 
      sent_date 
------------------------------- 
2016-02-23 22:13:33.858971+00 
2016-02-23 22:13:31.458917+00 
2016-02-23 22:13:26.186859+00 
2016-02-23 22:13:46.347431+00 
2016-02-23 22:22:33.483243+00 
2016-02-23 22:22:38.482727+00 
2016-02-23 22:22:35.929189+00 
2016-02-23 22:23:15.196597+00 
2016-02-23 22:24:19.257813+00 
2016-02-23 22:23:39.746997+00 
2016-02-23 22:24:14.382916+00 
2016-02-23 22:24:45.410786+00 
2016-02-23 22:23:58.929407+00 
2016-02-23 22:28:46.33302+00 
2016-02-23 22:28:52.141082+00 
2016-02-23 22:29:16.330865+00 
2016-02-23 22:58:33.798908+00 
2016-02-23 22:58:33.795771+00 
2016-02-24 03:06:11.60482+00 
2016-02-24 03:06:31.276288+00 
2016-02-24 03:07:07.885728+00 
2016-02-24 03:14:31.630305+00 
2016-02-24 03:14:31.632573+00 
2016-02-24 05:16:21.757124+00 
2016-02-24 05:16:21.760133+00 
2016-02-24 05:51:38.791875+00 
2016-02-24 05:51:38.794093+00 
2016-02-24 05:52:58.686893+00 
(28 rows) 

這從來沒有工作過,所以我猜我的查詢是錯誤的東西。

回答

0

您只在WHERE部分更改sent_date的時區。

你也可以做到這一點SELECT部分,如:

SELECT sent_date::timestamp with time zone AT TIME ZONE 'AEDT' 
    FROM jm 
    WHERE from_jid like '[email protected]%' 
    AND sent_date::timestamp with time zone AT TIME ZONE 'AEDT' BETWEEN '2016-02-23' AND '2016-02-29'; 
+0

謝謝@OcuS就是這樣 – Daniel