Re: query by partial timestamp

Поиск
Список
Период
Сортировка
От hubert depesz lubaczewski
Тема Re: query by partial timestamp
Дата
Msg-id 20130109180937.GB16056@depesz.com
обсуждение исходный текст
Ответ на query by partial timestamp  (Kirk Wythers <kwythers@umn.edu>)
Список 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 по дате отправления:

Предыдущее
От: Michael Nolan
Дата:
Сообщение: Re: query by partial timestamp
Следующее
От: Ben Chobot
Дата:
Сообщение: Re: How to store clickmap points?