17

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.

Johan
  • 74,508
  • 24
  • 191
  • 319
  • Do you mean you need one row for each of the individual dates between start and end in the result set? – Yuck Aug 25 '11 at 12:47
  • possible duplicate of [Get a list of dates between two dates](http://stackoverflow.com/questions/510012/get-a-list-of-dates-between-two-dates) – Jacob Aug 25 '11 at 12:47
  • If this is MySQL. If not, sorry for the quick dupe close vote :) – Jacob Aug 25 '11 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` – Johan Aug 25 '11 at 13:32

4 Answers4

29
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

Johan
  • 74,508
  • 24
  • 191
  • 319
5

Does this help you ?

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

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

Jean-Charles
  • 1,690
  • 17
  • 28
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.

John N
  • 1,755
  • 17
  • 21
0

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

  • 1
    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](/help/how-to-answer). – Community Jan 06 '23 at 17:49