【发布时间】:2017-01-06 14:58:27
【问题描述】:
我有一个具有时间值的 postgresql 表,但它存储时没有时区信息。我想将其转换为 UTC 时间,但以特定方式对其进行格式化。 我在某处有语法错误,我不知道如何修复它。我有以下 sql 查询:
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”引用。 我在 stackoverflow 上找到了另一篇建议使用 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 |
【问题讨论】:
-
你为什么首先使用
AT TIME ZONE 'UTC'?您的列不包含时区。 -
@pozs 我需要时间来转换为 UTC 格式。不知道还能怎么做。如果有更好的方法,我会全力以赴。
标签: sql postgresql utc