- Jump to 2nd last code block for:
*I want to be able to tell that
between 1/10/11 and 1/13/11 there is
a gap so the start and end date is*
not possible.
- Jump to last code block for:
*I want to return only
the datespans up to the first gap
encountered.*
First of all, here's a virtual table to discuss
create table spans (date1 datetime, date2 datetime);
insert into spans select '20110101', '20110110';
insert into spans select '20110113', '20110115';
insert into spans select '20110120', '20110130';
This is a query that will list, individually, all the dates in the calendar
declare @startdate datetime, @enddate datetime
select @startdate = '20110107', @enddate = '20110114'
select distinct a.date1+v.number
from spans A
inner join master..spt_values v
on v.type='P' and v.number between 0 and datediff(d, a.date1, a.date2)
-- we don't care about spans that don't intersect with our range
where A.date1 <= @enddate
and @startdate <= A.date2
Armed with this query, we can now test to see if there are any gaps, by
counting the days in the calendar against the expected number of days
declare @startdate datetime, @enddate datetime
select @startdate = '20110107', @enddate = '20110114'
select case when count(distinct a.date1+v.number)
= datediff(d,@startdate, @enddate) + 1
then 'No gaps' else 'Gap' end
from spans A
inner join master..spt_values v
on v.type='P' and v.number between 0 and datediff(d, a.date1, a.date2)
-- we don't care about spans that don't intersect with our range
where A.date1 <= @enddate
and @startdate <= A.date2
-- count only those dates within our range
and a.date1 + v.number between @startdate and @enddate
Another way to do this is to just build the calendar from @start
to @end up front and look to see if there is a span with this date
declare @startdate datetime, @enddate datetime
select @startdate = '20110107', @enddate = '20110114'
-- startdate+v.number is a day on the calendar
select @startdate + v.number
from master..spt_values v
where v.type='P' and v.number between 0
and datediff(d, @startdate, @enddate)
-- run the part above this line alone to see the calendar
-- the condition checks for dates that are not in any span (gap)
and not exists (
select *
from spans
where @startdate + v.number between date1 and date2)
The query returns ALL dates that are gaps in the date range @start - @end
A TOP 1
can be added to just see if there are gaps
To return all records that are before the gap, use the query as a
derived table in a larger query
declare @startdate datetime, @enddate datetime
select @startdate = '20110107', @enddate = '20110114'
select *
from spans
where date1 <= @enddate and @startdate <= date2 -- overlaps
and date2 < ( -- before the gap
select top 1 @startdate + v.number
from master..spt_values v
where v.type='P' and v.number between 0
and datediff(d, @startdate, @enddate)
and not exists (
select *
from spans
where @startdate + v.number between date1 and date2)
order by 1 ASC
)