rounding a timestamp to nearest x seconds

Поиск
Список
Период
Сортировка
От Andy Colson
Тема rounding a timestamp to nearest x seconds
Дата
Msg-id 4F577A32.60001@squeakycode.net
обсуждение исходный текст
Ответы Re: rounding a timestamp to nearest x seconds  (Daniele Varrazzo <daniele.varrazzo@gmail.com>)
Список pgsql-general
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

В списке pgsql-general по дате отправления:

Предыдущее
От: Gary Chambers
Дата:
Сообщение: Re: Fixing the loss of 'template1'
Следующее
От: Adrian Klaver
Дата:
Сообщение: Re: Fixing the loss of 'template1'