【发布时间】:2010-12-11 17:50:03
【问题描述】:
我需要在我的数据库上执行一些日期时间操作,这要求所有 created 时间戳具有相同的年份。我试过这个查询:
select created,cast (created -
to_timestamp(''||date_part('year', created), 'YYYY') +
timestamp '2010-01-01' as timestamp without time zone)
from table where created is not null limit 10;
...但结果只是部分令人满意:
created | timestamp
---------------------+---------------------
2010-08-29 12:44:05 | 2010-08-29 11:44:05
2007-06-21 13:49:22 | 2010-06-21 12:49:22
2007-06-21 15:02:33 | 2010-06-21 14:02:33
2007-06-21 15:05:26 | 2010-06-21 14:05:26
1999-09-30 13:00:00 | 2010-09-30 12:00:00
1997-06-07 13:00:00 | 2010-06-07 12:00:00
2010-06-15 20:51:01 | 2010-06-15 19:51:01
2006-08-26 15:33:02 | 2010-08-26 14:33:02
2009-12-15 11:07:06 | 2010-12-15 11:07:06
1997-06-28 13:00:00 | 2010-06-28 12:00:00
(10 rows)
如您所见,与原始 created 值相比,所有时间戳都是 -1 小时。更奇怪的是,最后一个(2010-12-15 11:07:06)不是。
created 列是timestamp without time zone 类型。
知道我做错了什么吗?
【问题讨论】:
标签: sql postgresql timestamp