Hi @RoyB09 ,
Please also refer below and check whether it is working to you. Thanks.
declare @Tbl1 table ( StudentID int, StartDate date, EndDate date, MonthsOn int )
insert @Tbl1 values
( 1, '20140101', '20140531', 4 ),
( 2, '20161001', '20170901', 11 ),
( 3, '20180301', '20200901', 30 )
declare @Tbl2 table ( StudentID int, TimeOffStart date, TimeOffEnd date, MonthsOff int )
insert @Tbl2 values
( 2, '20161101', '20161201', 1 ),
( 3, '20180501', '20191001', 17 )
;with cte as(
select StudentID,dateadd(MONTH,v.number,d.StartDate) adate
from @Tbl1 d
join master..spt_values v on v.type='P'
and v.number <= MonthsOn)
,cte1 as (
select StudentID,dateadd(MONTH,v.number,d.TimeOffStart) adate
from @Tbl2 d
join master..spt_values v on v.type='P'
and v.number < MonthsOff)
select StudentID,CONVERT(VARCHAR(7), adate, 126) Month from cte
except
select StudentID,CONVERT(VARCHAR(7), adate, 126) Month from cte1
Output:
StudentID Month
1 2014-01
1 2014-02
1 2014-03
1 2014-04
1 2014-05
2 2016-10
2 2016-12
2 2017-01
2 2017-02
2 2017-03
2 2017-04
2 2017-05
2 2017-06
2 2017-07
2 2017-08
2 2017-09
3 2018-03
3 2018-04
3 2019-10
3 2019-11
3 2019-12
3 2020-01
3 2020-02
3 2020-03
3 2020-04
3 2020-05
3 2020-06
3 2020-07
3 2020-08
3 2020-09
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Hot issues November--What can I do if my transaction log is full?
Hot issues November--How to convert Profiler trace into a SQL Server table