I need to select from a table that has a starts_on
field and an ends_on
field.
I need to pass start date and end date for filtering and retrieving those objects.
At the moment it's working and I use the following:
SELECT * FROM ***
WHERE ((starts_on >= START_DATE AND starts_on <= END_DATE) OR
(ends_on >= START_DATE AND ends_on <= END_DATE) OR
(starts_on <= END_DATE AND ends_on >= END_DATE))
ORDER BY starts_on, id
It looks a bit messy, but can't see an easy way to simplify it. Any idea?
I'm using postgres 9.1 as dbms.
Edit:
starts_on | timestamp without time zone |
ends_on | timestamp without time zone |
Ex: if one entry has starts_on = '2012/02/02' and ends_on '2012/02/05' I want the following behavior:
- if I filter by start date 2012/01/01 and end date 2012/03/01 I want the item to be returned
- if I filter by start date 2012/02/04 and end date 2012/03/01 I want the item to be returned
- if I filter by start date 2012/02/05 and end date 2012/03/01 I want the item to be returned
- if I filter by start date 2012/02/04 and end date 2012/02/04 I want the item to be returned
- if I filter by start date 2012/02/06 and end date 2012/03/01 I want the item to NOT be returned
- if I filter by start date 2012/01/01 and end date 2012/02/01 I want the item to NOT be returned