简体   繁体   中英

Selecting rows with date between start and end date

I have a timestampz column in postgresql table and I wish to retrieve entries lying within a specific time frame

I tried using

SELECT * FROM table where date BETWEEN '2012-01-01' AND '2012-01-05'

But the above query fails to retrieve some rows that fall within the specified date, so I tried something like this

SELECT * FROM table WHERE date BETWEEN '2012-01-01 00:00:00' AND '2012-01-05 23:59:59'

This works fine, but Is there a better way to active this?

You can try:

SELECT * 
FROM table 
where date::DATE BETWEEN '2012-01-01'::DATE AND '2012-01-05'::DATE

SQL Fiddle

Not much better but cleaner i think.

select *
from table
where 
    date >= '2012-01-01' 
    and 
    date < '2012-01-05'::date + 1

It will cover up to '2012-01-05 23:59:59.999999'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM