18

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.

4
  • Do you mean you need one row for each of the individual dates between start and end in the result set? Commented Aug 25, 2011 at 12:47
  • possible duplicate of Get a list of dates between two dates Commented Aug 25, 2011 at 12:47
  • If this is MySQL. If not, sorry for the quick dupe close vote :) Commented Aug 25, 2011 at 12:47
  • 5
    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 enddate Commented Aug 25, 2011 at 13:32

4 Answers 4

31
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

Sign up to request clarification or add additional context in comments.

2 Comments

+1 - But I would change enddate to COALESCE(enddate,now())
If the enddate is defined as NOT NULL, good suggestion.
6

Does this help you ?

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

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

Comments

1

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.

Comments

0

SELECT Count (DISTINCT id) FROM Table WHERE rqmnt_start_date::TIMESTAMP >= '27/01/2022' and rqmnt_end_date::TIMESTAMP <= '30/11/2022'

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.