简体   繁体   中英

convert time to 100th / 10th of a second resolution

The pattern 'YYYY-MM-DD HH24:MI:SS' gets time to seconds resolution.

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

couldn't figure out from documentation here

'YYYY-MM-DD HH24:MI:SS.MS' would give '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.')

If it's an option, it seems like it would be easier to just go with three decimal places...

SQL DEMO

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM