Postgresql - Select between specific Time and Date Range

7.4k Views Asked by At

I'm trying to get some data from my tables - I need only a particular time of day ( between 22:00 PM one night and 06:00 AM the next morning) over a fixed date range.

This is what I have so far:

create new_table
as select table.v1, table.localminute from table 
where date(table.localminute) between
date('2013-11-01 00:00:00-05') and date('2014-12-01 00:00:00-05') 
and hour(table.localminute) between 22 and 6

I'm not sure how to just get the data for between the hours of 22:00:00-05 and 06:00:00-05 each day.

I also thought about using wildcards for the dates but either it doesn't work or my implementation wasn't right. Any help will be appreciated!

I used info from the link below to set this up as it is - not sure what I need to do.

Select between date range and specific time range

1

There are 1 best solutions below

3
On BEST ANSWER
SELECT v1, localminute 
FROM "table" 
WHERE localminute BETWEEN '2013-11-01'::date AND '2014-12-01'::date 
AND (extract('hour' from localminute) >= 22 OR extract('hour' from localminute) < 6);