Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 373
Akash Avatar asked Dec 06 '25 08:12

Akash


2 Answers

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'

like image 137
Clodoaldo Neto Avatar answered Dec 07 '25 22:12

Clodoaldo Neto


You can try:

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

SQL Fiddle

like image 35
Robert Avatar answered Dec 07 '25 20:12

Robert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!