UPDATE: After a good discussion with Ada, I see how his query is the optimal solution. While my answer below will work, it's redundant. I'll leave this answer here for history's sake. +1 to Ada's answer.
As I understand the question, he wants to retrieve any tasks that were being worked on at any point between the dates specified. This implies partially overlapping tasks as well as tasks which started and finished during the time interval.
Two time slots overlap if:
- The start time on task 1 falls anywhere between the start and end times of task 2 OR
- The end time on task 1 falls anywhere between the start and end times of task 2 OR
- Start and end times of
I have used this method successfully in a reservations system to prevent any overlap in bookings.
So far in the answers I haven't seen a query that checks both of these conditions. Here's my attempt:
WHERE ( (StartDate >= @FromDate) AND (StartDate <= @EndDate) )
OR ( (EndDate >= @FromDate) AND (EndDate <= @EndDate) )
OR ( (StartDate <= @EndDate) AND (EndDate >= @StartDate) )