14
votes

I need a query in SQL.
If I have two columns STARTDATE and END_DATE.
I want to select a all rows where a date falls between these two dates.

e.g.: startdate = 1/1/2011 AND enddate = 2/2/2011.

3
Do you mean you need one row for each of the individual dates between start and end in the result set?Yuck
possible duplicate of Get a list of dates between two datesJacob
If this is MySQL. If not, sorry for the quick dupe close vote :)Jacob
Too all the close voters, how is this not a real question? It is clear as glass that the OP wants to do SELECT * FROM t1 WHERE ? BETWEEN startdate AND enddateJohan

3 Answers

24
votes
SELECT * FROM table1 
WHERE '2011-01-01' BETWEEN table1.startdate AND table1.enddate

Replace the explicit date either with now() or a parameter or whatever.

If the enddate is not defined as NOT NULL you can do:

SELECT * FROM table1 
WHERE '2011-01-01' BETWEEN table1.startdate AND COALESCE(table1.enddate, NOW())

See: http://www.1keydata.com/sql/sql-coalesce.html

4
votes

Does this help you ?

select * 
from table 
where START_DATE < NOW() AND END_DATE > NOW()

Depending on the database, use CURRENT_TIMESTAMP() or TODAY()

1
votes

Do you mean this:

select *
from mytable
where start_date >= '01/01/2011'
and end_date <= '02/01/2011'

Until you do more to clarify your question, it's hard for us to provide better answers.