2017-01-06 76 views
0

我有一個postgresql表,它具有時間值,但沒有時區信息存儲。我想將其轉換爲UTC時間,但是要將其格式化爲特定方式。 我有一個語法錯誤的地方,我不知道如何解決它。我有以下SQL查詢:如何從時間戳中刪除UTC信息

testdb=# select id, starttime at TIME ZONE 'UTC',endtime AT TIME ZONE 'UTC', dtc, etcd from fre order by id; 

而且像這樣返回數據:

 id  | timezone | timezone | dtc |  etcd   
-------------+-------------+-------------+-----------+------------------------- 
143322 | 13:00:00+00 | 00:00:00+00 | 0000000 | 8899703 
990222 | 05:00:00+00 | 05:00:00+00 | 0000000 | 45007 
452256 | 05:00:00+00 | 05:00:00+00 | 0000000 | 33303 
123118 | 05:08:00+00 | 00:00:00+00 | 1111100 | 8899701 

我想刪除的開始時間/結束字段中的「+00」的參考。 我發現這裏的計算器另一篇文章使用TO_CHAR()方法的建議,並用這個例子:

testdb=# select to_char(now(), 'HH24:MI:SS'); 
to_char 
---------- 
09:55:48 
(1 row) 

所以我適應它,並試圖這樣:

testdb=# select to_char(now() AT TIME ZONE 'UTC', 'HH24:MI:SS'); 
to_char 
---------- 
14:55:58 
(1 row) 

現在,我想將此應用於我的原始查詢,如下所示:

testdb=# select to_char(starttime AT TIME ZONE 'UTC', 'HH24:MI:SS') from fre; 
ERROR: function to_char(time with time zone, unknown) does not exist 
LINE 1: select to_char(starttime AT TIME ZONE 'UTC', 'HH24:MI:SS') f... 
      ^
HINT: No function matches the given name and argument types. You might need to add explicit type casts. 

我不知道我在做什麼錯。

編輯1

在情況下,它可以幫助...

testdb=# \d+ fre; 
          Table "public.fre" 
    Column |   Type   | Modifiers | Storage | Description 
-----------+------------------------+-----------+----------+------------- 
id  | character varying(40) | not null | extended | 
etcd  | character varying(40) | not null | extended | 
starttime | time without time zone |   | plain | 
endtime | time without time zone |   | plain | 
startdate | date     |   | plain | 
enddate | date     |   | plain | 
dtc  | bit(7)     |   | extended | 
+0

爲什麼你使用'AT TIME ZONE「 UTC''在第一位?您的列不包含時區。 – pozs

+0

@pozs我需要時間轉換爲UTC格式。不知道如何去做。如果有更好的辦法,我全都是耳朵。 – Happydevdays

回答

0

轉換回時間戳無時區:

(starttime at TIME ZONE 'UTC')::timestamp without time zone