我可以将时间戳的格式设置为秒,如下所示。
to_char('2017-02-20 08:22:44.166'::timestamp, 'YYYY-MM-DD HH24:MI:SS') --> 2017-02-20 08:22:44
我需要将上面的时间戳格式化为100秒和10秒。
- > 2017-02-20 08:22:44.17
(格式化为1/100秒)
- > 2017-02-20 08:22:44.2
(格式化为秒的1/10)
我尝试了postgres的做法,如下所示,但这在我使用
的Netezza平台中出错了select '2017-02-20 08:22:44.166'::timestamp(2), '2017-02-20 08:22:44.166'::timestamp(1);
ERROR [42000] ERROR: 'select '2017-02-20 08:22:44.166'::timestamp(2), '2017-02-20 08:22:44.166'::timestamp(1) LIMIT 1000'
error ^ found "(" (at char 44) expecting a keyword
答案 0 :(得分:1)
这不是很漂亮,但它确实适用于PostgreSQL,我相信它可以在Netezza上运行。 (不幸的是我无法访问Netezza盒子,所以我只是依赖于文档):
select
'2017-02-20 08:22:44.166'::timestamp,
to_char('2017-02-20 08:22:44.166'::timestamp, 'YYYY-MM-DD HH24:MI:SS.') ||
round(to_char('2017-02-20 08:22:44.166'::timestamp, 'MS')::float / 10) as hundredths,
to_char('2017-02-20 08:22:44.166'::timestamp, 'YYYY-MM-DD HH24:MI:SS.') ||
round(to_char('2017-02-20 08:22:44.166'::timestamp, 'MS')::float / 100) as tenths;
timestamp | hundredths | tenths
-------------------------+------------------------+-----------------------
2017-02-20 08:22:44.166 | 2017-02-20 08:22:44.17 | 2017-02-20 08:22:44.2
(1 row)