Convert list of dates in a date range in SQL Server

For ex:

enter image description here

  • How do you obtain those intervals? I see 7 records on the left and only 6 values on the right.
    – Alexei
    Commented Feb 15, 2018 at 9:39

1 Answer 1


It's classic "islands" problem.

Here is "classic" solution:

declare @t table (dt date);

insert into @t(dt) values 

with c as 
  select dt, dateadd(day, -1 * dense_rank() over(order by dt), dt) as grp 
  from @t 
select min(dt) as start_range, max(dt) as end_range 
from c 
group by grp;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.