phoenix_ (5/29/2013)
In the meantime I created something like this...
declare @StartDate date
set @StartDate = '2013-01-03'
declare @EndDate date
set @EndDate = '2013-02-21'
declare @DateCalc date
declare @WeekStartDate date
set @WeekStartDate = DATEADD(ww, DATEDIFF(ww,0,@StartDate), 0)
set @DateCalc = @StartDate
WHILE (@WeekStartDate <= @EndDate )
begin
select case when @StartDate > @WeekStartDate then DatePart(ww,@StartDate) else DatePart(ww,@WeekStartDate) end as WeekNum, @WeekStartDate as WeekStartDate, DATEADD(dd, 6, @WeekStartDate) as EndDate;
set @WeekStartDate = DATEADD(dd, 7, @WeekStartDate)
set @StartDate = @WeekStartDate
END
Is it possible somehow to convert it to 'one' select statement so I will have only one result?
Yes. It is possible to convert it to single SELECT statement. But, I would advise against it.
We have provided better performing set-based solutions and you have used a WHILE loop which works on a row by row basis.
One more observation after executing your code: Your expected results are different from the results of this query.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/