I'm not sure I understand your question. For a given date, you want to see the number of rows (events) returned??
So for the 2/1/09 you should see 1, for the 2/2/09 you should see 0???
SQL:
declare @temp table (evt varchar(10), start datetime, [end] datetime)
insert into @temp values('e1', '1/1/2009', '2/1/2009')
insert into @temp values('e2', '1/15/2009', '1/31/2009')
insert into @temp values('e3', '8/15/2008', '1/16/2009')
insert into @temp values('e4', '2/3/2009', '2/15/2009');
select * from @temp where start < '2/1/2009' and [end] >= '2/1/2009'
select * from @temp where start < '2/2/2009' and [end] >= '2/2/2009'
C# / linq:
public class Event
{
public string eventID;
public DateTime start;
public DateTime end;
}
static void Main(string[] args)
{
IList<Event> events = new List<Event>();
events.Add(new Event { eventID = "e1", start = new DateTime(2009, 1, 1), end = new DateTime(2009, 2, 1) });
events.Add(new Event { eventID = "e2", start = new DateTime(2009, 1, 15), end = new DateTime(2009, 1, 31) });
events.Add(new Event { eventID = "e3", start = new DateTime(2008, 8, 15), end = new DateTime(2009, 1, 16) });
events.Add(new Event { eventID = "e4", start = new DateTime(2009, 2, 3), end = new DateTime(2009, 2, 15) });
DateTime eventDate = new DateTime(2009, 2, 1);
var available = events.Where(e => e.start.CompareTo(eventDate) < 1 && e.end.CompareTo(eventDate) > -1);
Console.WriteLine(available.Count());
eventDate = new DateTime(2009, 2, 2);
available = events.Where(e => e.start.CompareTo(eventDate) < 1 && e.end.CompareTo(eventDate) > -1);
Console.WriteLine(available.Count());
eventDate = new DateTime(2009, 1, 16);
available = events.Where(e => e.start.CompareTo(eventDate) < 1 && e.end.CompareTo(eventDate) > -1);
Console.WriteLine(available.Count());
Console.ReadLine();
}
EDIT: It's not pretty, but this SQL will give you the result you have asked for:
declare @temp table (evt varchar(10), start datetime, [end] datetime)
declare @result table (available datetime)
insert into @temp values('e1', '1/1/2009', '2/1/2009')
insert into @temp values('e2', '1/15/2009', '1/31/2009')
insert into @temp values('e3', '8/15/2008', '1/16/2009')
insert into @temp values('e4', '2/3/2009', '2/15/2009');
declare @start datetime
declare @end datetime
set @start = '1/1/2009'
set @end = '2/16/2009'
while @start < dateadd(day, 1, @end)
begin
declare @rowCount int
select @rowCount = count(*) from @temp where start <= @start and [end] >= @start
if @rowCount = 0
insert into @result values(@start)
set @start = dateadd(day, 1, @start)
end
select * from @result