Convert list of dates in a date range in SQL Server
For ex:
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It only takes a minute to sign up.
Sign up to join this communityIt's classic "islands" problem.
Here is "classic" solution:
declare @t table (dt date);
insert into @t(dt) values
('20180202'),
('20180203'),
('20180204'),
('20180205'),
('20180209'),
('20180212'),
('20180213');
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;