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.
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())
SELECT * FROM t1 WHERE ? BETWEEN startdate AND enddate
– Johan