You can create this table using a common table expression (cte for short) tally table. This involves manually specifying the first 10 rows (the select t from values... bit) and then cross joining this to itself several times in a second cte to exponentially create more rows. Because I have 6 cross joins (the t t1, t t2, t t3... part) I am generating a million rows (10^6), then working out how many I actually need in the top clause and then using row_number to generate an iterative list that is added as days to your period start date.
The output of this is a table of dates you can apply functions to, that will even work across multiple financial years. I would recommend you use this to create a Dates lookup table rather than running every single time:
declare @DateStart date = '20160701'
,@DateEnd date = '20170630';
with t(t) as (select t from (values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) t(t))
,d(d) as (select top(datediff(d,@DateStart,@DateEnd)+1) dateadd(d,row_number() over (order by (select null))-1,@DateStart) from t t1,t t21,t t31,t t4,t t5,t t6)
select ((dense_rank() over (order by dateadd(m,datediff(m,0,d),0)) - 1) % 12) + 1 as [Period]
,dense_rank() over (partition by dateadd(m,datediff(m,0,d),0)
order by case when dateadd(d,1-datepart(dw,d),d) < dateadd(m,datediff(m,0,d),0)
then dateadd(m,datediff(m,0,d),0)
else dateadd(d,1-datepart(dw,d),d)
end) as [Week]
,case when dateadd(d,1-datepart(dw,d),d) < dateadd(m,datediff(m,0,d),0)
then dateadd(m,datediff(m,0,d),0)
else dateadd(d,1-datepart(dw,d),d)
end as StartDate
,case when dateadd(d,7-datepart(dw,d),d) > dateadd(m,datediff(m,0,d)+1,0)
then dateadd(d,-1,dateadd(m,datediff(m,0,d)+1,0))
else dateadd(d,7-datepart(dw,d),d)
end as EndDate
,d as DayOfWeek
,year(d) as [Year]
from d
order by d;
Output:
+--------+------+-------------------------+-------------------------+------------+------+
| Period | Week | StartDate | EndDate | DayOfWeek | Year |
+--------+------+-------------------------+-------------------------+------------+------+
| 1 | 1 | 2016-07-01 00:00:00.000 | 2016-07-02 00:00:00.000 | 2016-07-01 | 2016 |
| 1 | 1 | 2016-07-01 00:00:00.000 | 2016-07-02 00:00:00.000 | 2016-07-02 | 2016 |
| 1 | 2 | 2016-07-03 00:00:00.000 | 2016-07-09 00:00:00.000 | 2016-07-03 | 2016 |
| 1 | 2 | 2016-07-03 00:00:00.000 | 2016-07-09 00:00:00.000 | 2016-07-04 | 2016 |
| 1 | 2 | 2016-07-03 00:00:00.000 | 2016-07-09 00:00:00.000 | 2016-07-05 | 2016 |
| 1 | 2 | 2016-07-03 00:00:00.000 | 2016-07-09 00:00:00.000 | 2016-07-06 | 2016 |
| 1 | 2 | 2016-07-03 00:00:00.000 | 2016-07-09 00:00:00.000 | 2016-07-07 | 2016 |
| 1 | 2 | 2016-07-03 00:00:00.000 | 2016-07-09 00:00:00.000 | 2016-07-08 | 2017 |
.
.
.
| 12 | 5 | 2017-06-25 00:00:00.000 | 2017-07-01 00:00:00.000 | 2017-06-28 | 2017 |
| 12 | 5 | 2017-06-25 00:00:00.000 | 2017-07-01 00:00:00.000 | 2017-06-29 | 2017 |
| 12 | 5 | 2017-06-25 00:00:00.000 | 2017-07-01 00:00:00.000 | 2017-06-30 | 2017 |
+--------+------+-------------------------+-------------------------+------------+------+