Solved it myself;
declare @firstDate DateTime
declare @today DateTime
declare @checkDate DateTime
DECLARE @Yearweek TABLE
(
YearWeekValue int
)
set @today = getDate()
SELECT @firstDate = min([occurred])
FROM MyTable
set @checkDate = @firstDate
while @checkDate < @today
begin
print (datepart(year,@checkDate) * 100 + datepart(isowk, @checkDate))
insert into @Yearweek
select (datepart(year,@checkDate) * 100 + datepart(isowk, @checkDate))
set @checkDate = DATEADD(dd, 7, @checkDate)
end
select * from @Yearweek
method used;
- determine firstdate in data (I know the MIN-function can lead/will to tablescan and that index on datetime field used in order by with a select top 1 is faster)
- determine today
- have a datetime value that runs between the two dates
- while the inbetween date is not larger than today
- insert the yearweek value in the created temp table
- add 7 days to the in between date to miss not any week
in the end select the data from the temp table.