Convert a list of dates to date ranges in SQL Server -
i have query following:
select [date] [tablex] order [date]
the result is:
2016-06-01 2016-06-03 2016-06-10 2016-06-11
how can following pairs?
from 2016-06-01 2016-06-03 2016-06-03 2016-06-10 2016-06-10 2016-06-11
a little tricky solution sql 2008.
declare @tbl table(dt datetime) insert @tbl values ('2016-06-01'), ('2016-06-03'), ('2016-06-10'), ('2016-06-11') ;with cte ( select dt, row_number() over(order dt) rn --add number @tbl ), newtbl ( select t1.dt start, t2.dt [end] cte t1 inner join cte t2 on t1.rn+1=t2.rn ) select * newtbl
the result wish.