I have two columns in the database, project_start and project_end, and I’m in passing two fields (both optional) via a report (coded in ASP).
The pseudo code below is what I currently have:
(project_start <= search_start AND project_end <= search_start)
AND
(project_start <= search_end AND project_end >= search_end)
However what the above doesn’t pick up are projects which start before the start date, but end after the start date, projects which start after the start date but end after the end date, and also projects which start before the start date and end after the end date :S
Below is my code:
if (Request.QueryString["DateFrom"] != null && Request.QueryString["DateFrom"] != "")
{
DateTime dateFromFilter;
dateFromFilter = DateTime.Parse(Request.QueryString["DateFrom"]);
Q = from qua in qTable
where qua.Start_Date.Date <= dateFromFilter.Date
&& qua.End_Date.Date >= dateFromFilter.Date
select qua;
}
//filter to date
if (Request.QueryString["DateTo"] != null && Request.QueryString["DateTo"] != "")
{
DateTime dateToFilter;
dateToFilter = DateTime.Parse(Request.QueryString["DateTo"]);
Q= from qua in qTable
where qua.Start_Date.Date <= dateToFilter.Date
&& qua.End_Date.Date >= dateToFilter.Date
select qua;
}
If you want the projects that overlap with any part of your search range, try this:
I know it looks “too simple” but it does in fact cover all the cases:
Update
To handle optional paramaters: