[英]convert time to 100th / 10th of a second resolution
The pattern 'YYYY-MM-DD HH24:MI:SS'
gets time to seconds resolution. 模式'YYYY-MM-DD HH24:MI:SS'
获得时间达到秒分辨率。
select to_char('2017/02/20 08:23:58.267'::timestamp, 'YYYY-MM-DD HH24:MI:SS');
=> 2017-02-20 08:23:58
What patterns would get to 10th of a second and 100th of a second resolutions? 哪种模式会达到十分之一秒的分辨率和百分之一秒的分辨率?
100th second resolution => 2017-02-20 08:23:58.27
10th second resolution => 2017-02-20 08:23:58.3
'YYYY-MM-DD HH24:MI:SS.MS'
would give '2017-02-20 08:23:58.270'. 'YYYY-MM-DD HH24:MI:SS.MS'
将为'2017-02-20 08:23:58.270'。 You could just truncate that string by one or two characters. 您可以将字符串截断一个或两个字符。 Or, for accurate rounding, you could do a separate conversion using the pattern 'SS.MS'
, convert that string to a float, round it, convert it back to a string, and append that to the result of to_char(ts, 'YYYY-MM-DD HH24:MI.')
或者,为了精确舍入,您可以使用模式'SS.MS'
进行单独的转换,将字符串转换为浮点数,将其舍入,将其转换回字符串,然后将其附加到to_char(ts, 'YYYY-MM-DD HH24:MI.')
If it's an option, it seems like it would be easier to just go with three decimal places... 如果可以选择的话,只用小数点后三位似乎会更容易...
WITH cte as (
SELECT '2017/02/20 08:23:58.267'::timestamp as t1
)
SELECT to_char(t1, 'YYYY-MM-DD HH24:MI:SS'),
to_char(t1, 'YYYY-MM-DD HH24:MI:SS MS') millisecond,
to_char(t1, 'YYYY-MM-DD HH24:MI:SS US') microsecond
FROM cte
OUTPUT 输出值
You can use date_part('microseconds', t1)
and and date_part('seconds', t1)
with some math to round the desire value. 您可以通过一些数学运算来使用date_part('microseconds', t1)
和date_part('seconds', t1)
来舍入期望值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.