Re: Rounding datetimes

From: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
To: jws <jsacksteder(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Rounding datetimes
Date: 2007-05-22 17:26:07
Message-ID: 465327AF.9020702@pinpointresearch.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

jws wrote:
> Is there a way to round an interval to the nearest minute...

Yes. See date_trunc function. Of course this truncates down to the
specified unit. If you want to round up/down to the nearest minute I
suppose you could just add '30 seconds'::interval before truncating.

select
now(),
date_trunc('minute', now()),
date_trunc('minute', now()+'30 seconds'::interval);

-[ RECORD 1 ]-----------------------------
now | 2007-05-22 10:25:37.706279-07
date_trunc | 2007-05-22 10:25:00-07
date_trunc | 2007-05-22 10:26:00-07

Cheers,
Steve

In response to

Browse pgsql-general by date

  From Date Subject
Next Message sudhir 2007-05-22 17:27:59 Re: Lock table, Select for update and Serialization error
Previous Message Robert Fitzpatrick 2007-05-22 17:23:35 Re: Permance issues with migrated db