Re: query by partial timestamp - Mailing list pgsql-general

From hubert depesz lubaczewski
Subject Re: query by partial timestamp
Date
Msg-id 20130109180937.GB16056@depesz.com
Whole thread Raw
In response to query by partial timestamp  (Kirk Wythers <kwythers@umn.edu>)
List pgsql-general
On Tue, Jan 08, 2013 at 04:19:59PM -0600, Kirk Wythers wrote:
> I have a column of type TIMESTAMP, I'd like to query all records from 2011. If it were text I could use a partial
suchas: 
>
> WHERE
>     text ~ '2011'
>
> There must be a simple way to pull the year part out of a timestamp format. Thanks in advance.

using partial checks (like extract, date_part, or even casting field to
date) will have problem with index usage.
the best way to handle it, is to write the parameters using date
arithmetic.
like:
where column >= '2011-01-01' and column < '2012-01-01'

do not be tempted to do:
where column >= '2011-01-01' and column <='2011-12-31'
which is very bad idea, and will cause data loss.

More on index usage:
http://www.depesz.com/2010/09/09/why-is-my-index-not-being-used/

Best regards,

depesz

--
The best thing about modern society is how easy it is to avoid contact with it.
                                                             http://depesz.com/


pgsql-general by date:

Previous
From: Michael Nolan
Date:
Subject: Re: query by partial timestamp
Next
From: Ben Chobot
Date:
Subject: Re: How to store clickmap points?