Search Postgresql Archives

rounding a timestamp to nearest x seconds

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Took me a while to figure this out, thought I'd paste it here for others to use:

create or replace function round_timestamp(timestamp, integer) returns timestamp as $$ select date_trunc('minute', $1) + cast(round(date_part('seconds', $1)/$2)*$2 || ' seconds' as interval);
$$ language sql immutable;


If you pass 10 to the second argument, it'll round the timestamp to the nearest 10 seconds. Pass 5 to round to nearest 5 seconds, etc..

-Andy

--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux