I'm surprised this hasn't come up yet.
In T-SQL, I need to find the intervals (defined by startDateTime and endDateTime) that overlap with daily interval (say 9am-5pm).
For example, with table:
CREATE TABLE [dbo].[Interval](
[startDateTime] [datetime] NOT NULL,
[endDateTime] [datetime] NOT NULL
)
Solution would be the procedure that returns only overlapping intervals:
CREATE PROCEDURE FindIntervals
-- Add the parameters for the stored procedure here
@from varchar(5) = '9:00',
@to varchar(5) = '17:00'
AS
BEGIN
select * from Interval
where ...
END
GO
EDIT: Example intervals:
- Sep 7 2011 8:00 AM - Sep 7 2011 8:30 PM
- Sep 7 2011 11:00 AM - Sep 7 2011 1:00 PM
- Sep 7 2011 1:00 PM - Sep 7 2011 6:00 PM
- Sep 9 2011 8:00 AM - Sep 9 2011 8:30 PM
- Sep 9 2011 11:00 AM - Sep 9 2011 1:00 PM
- Sep 9 2011 1:00 PM - Sep 9 2011 6:00 PM
So, for given interval "nine to five", 2, 3, 5 and 6 should be returned, as they overlap the given input.
But,
- Sep 9 2011 8:00 AM - Sep 10 2011 8:30 PM
also fits, because it includes entire day.
Please, I need help with matching string and datetime values in T-SQL, not abstract "less then"/"greater then" solutions.