Re: select date between - PostgreSQL 9.5

Поиск
Список
Период
Сортировка
От Daevor The Devoted
Тема Re: select date between - PostgreSQL 9.5
Дата
Msg-id CAAZnbVr9DEyTp2sqJKdC7zwSt77tH5RGnq9vOjwN4ssr8_BeAw@mail.gmail.com
обсуждение исходный текст
Ответ на Re: select date between - PostgreSQL 9.5  (Patrick B <patrickbakerbr@gmail.com>)
Список pgsql-general

On Wed, Sep 14, 2016 at 4:49 AM, Patrick B <patrickbakerbr@gmail.com> wrote:


2016-09-14 13:17 GMT+12:00 David Rowley <david.rowley@2ndquadrant.com>:
On 14 September 2016 at 12:20, Patrick B <patrickbakerbr@gmail.com> wrote:
> I want to select all rows that have been modified from now to 4 months ago.
>
> I've used these queries:
>
>> select
>>         modified_date,
>> from
>>         clients
>> WHERE
>> modified_date BETWEEN '2016-06-13' AND '2016-09-13'
>

Going by my clock here 2016-06-13 was just over 3 months ago, not 4.


>> select
>>         modified_date,
>> from
>>         clients
>> WHERE
>> modified_date >='2016-06-13' AND modified_date < '2016-09-13'
>
>
>
> But it didn't work... it returns 0 rows.... but there are rows to be shown:
>
>
>> select modified_date from clients ORDER BY modified_date ASC
>
>
>
>> modified_date
>> -------------------
>> 2015-07-11 17:23:40
>> 2016-09-13 20:00:51
>> 2016-09-13 20:00:51
>> 2016-09-13 20:00:51
>> 2016-09-13 20:00:51
>
>
>
> What am I doing wrong?

None of those dates are between your specified date range. If you want
to include all of 2016-09-13 timestamps, then you'd better do <
'2016-09-14' since < '2016-09-13' will only cover timestamps on the
12th or before.


--
 David Rowley                   http://www.2ndQuadrant.com/
 PostgreSQL Development, 24x7 Support, Training & Services


Thanks guys...

I've used < and >

not sure why wasn't working before :(

Thanks!
Patrick


It didn't work before because, as excellently pointed out by Vitaly Burovoy, because

modified_date BETWEEN '2016-06-13' AND '2016-09-13'

is evaluated as

modified_date >= '2016-06-13 00:00:00' AND modified_date <= '2016-09-13 00:00:00'

None of your timestamps falls in that range. '2016-09-13 20:00:51'  is 20 hours and 51 seconds after the end of this range, and '2015-07-11 17:23:40' is more than a year before it.

Similar logic applies to modified_date >= '2016-06-13 00:00:00' AND modified_date < '2016-09-13 00:00:00'

Now, the reason it is working for you now, is probably because you're in a timezone where it is already 2016-09-14, and your WHERE clause now reads:

modified_date >= '2016-06-14 00:00:00' AND modified_date < '2016-09-14 00:00:00'

with the effect that the timestamp '2016-09-13 20:00:51' now falls within the range of your new WHERE clause.

At least, that's my suspicion.

Kind regards,
Na-iem Dollie

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

Предыдущее
От: Adrian Klaver
Дата:
Сообщение: Re: Installing 9.6 RC on Ubuntu [Solved]
Следующее
От: Raymond O'Donnell
Дата:
Сообщение: Re: Installing 9.6 RC on Ubuntu