Hi @Victor Cordova ,
Welcome to Microsoft Q&A!
Please refer below and check whether it is helpful to you.
DECLARE @StartDate varchar(20)= '01-APR-2015',
@endDate varchar(20)='30-APR-2021'
DECLARE insert_data CURSOR FOR
with CTE AS
(
Select cast(@StartDate as date) As StartDate,
Case when datediff(year,cast(@StartDate as date) ,cast(@EndDate as date))>1 then
cast(DATEADD(yy, DATEDIFF(yy, 0, cast(@StartDate as date)) + 1, -1) as date) end EndDate
UNION ALL
Select dateadd(day,1,EndDate) As StartDate,
case when datediff(year,EndDate,cast(@EndDate as date) )>1 then
cast(DATEADD(yy, DATEDIFF(yy, 0, dateadd(day,1,EndDate)) + 1, -1) as date)
else cast(@endDate as date) end As EndDate
From CTE
where datediff(year,EndDate,cast(@EndDate as date))<>0
)
select UPPER(format(StartDate,'dd-MMM-yy')) STARTDATE,UPPER(format(EndDate,'dd-MMM-yy')) ENDDATE
from CTE
OPEN insert_data
DECLARE @newStartDate DATE,@newEndDate DATE
FETCH NEXT FROM insert_data INTO @newStartDate,@newEndDate
WHILE @@FETCH_STATUS=0
BEGIN
insert into TABLEWHERESAVEINFO(Key, Value)
select Key, Value
from TABLE1 ii left outer join
(
select * from pedimp
where DATEFIELD between @newStartDate and @newEndDate
) TABLE2 on table1.ID = table2.ID
FETCH NEXT FROM insert_data INTO @newStartDate,@newEndDate
END
CLOSE insert_data
DEALLOCATE insert_data
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.