I have a week number and year and i need to display "total for mm/dd/yy to mm/dd/yy in a row of my ssrs report. My week starts with Monday. For example if my week number is '2' and year is '2010' then I have to display "total for 01/04/2010 to 01/10/2010 in my ssrs column. how to do this?
-
What's the range of dates for week '1'? I prefer a calendar table for tasks like that: http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html – dnoeth May 11 '15 at 06:46
-
1for week1 12/28/2009 to 01/03/2010 – Alias Varghese May 11 '15 at 06:50
-
@AliasVarghese NOT correct, iso_week 1 is 2010-01-04 to 2010-01-10 – t-clausen.dk May 11 '15 at 10:56
5 Answers
SQL Server has a DATEPART function which calculates the ordinal week number of a year. However, you have to call DATEFIRST before this to define which day of the week represents the start of the week. In your case, you have stated that the start of your week is Monday (i.e. 1).
SET DATEFIRST 1;
SELECT SUM([your data column])
FROM [your table]
WHERE DATEPART(WEEKNUM, [your date column])=[your week parameter]
AND DATEPART(YEAR, [your date column])=[your year parameter]

- 1,598
- 11
- 15
-
I don't understand why you get a downvote, I voted you up. You use `datefirst` (bravo!) and `datepart` (bravo!), and not all that crappy hacks which others write. Only he wanted also to see beginning and end of the week. this is missing, but it is not much. – SQL Police Jun 11 '15 at 14:59
-
Try this
declare @year char(4) = '2010'
declare @week int = 2
declare @fromdate datetime
declare @todate datetime
set @fromdate = DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @year) + (@week-1), 7);
set @todate = DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @year) + (@week-1), 6) ;
;WITH dates AS
(
SELECT CONVERT(datetime,@fromDate) as Date
UNION ALL
SELECT DATEADD(d,1,[Date])
FROM dates
WHERE DATE < @toDate
)
select * from dates

- 2,200
- 13
- 14
-
1
-
**PLEASE**! a) This anser is not correct, b) and it is awful ! declare @year char(4)` ==> Come on, that's worst style ever! – SQL Police Jun 11 '15 at 14:56
Your description is not american standard nor isoweek. Seems like a mix of those. I never heard of that as a standard. It is nearly isoweek. So that is what this answer is based on.
Calculating iso year is a bit tricky, you can read about it here:
This is the syntax you need:
DECLARE @year int = 2010
DECLARE @week int = 2
;WITH CTE AS
(
SELECT
dateadd(wk, datediff(wk, - @week * 7,
cast(cast(@year as char(4)) as datetime) - 5), 0) startofweek
)
SELECT
replace('total for ' + convert(char(10), startofweek, 110)
+ ' to ' + convert(char(10), dateadd(day, 6, startofweek) , 110), '-', '/')
FROM CTE
Result:
total for 01/11/2010 to 01/17/2010
Isoweek 2 in 2010 is 2010-01-11

- 1
- 1

- 43,517
- 12
- 59
- 92
-
This is not the same definition as the ISO week, 01/04/2010 is in ISO week 1 :-) – dnoeth May 11 '15 at 09:26
Try setting DATEFIRST (https://msdn.microsoft.com/en-ie/library/ms181598.aspx)
SET DATEFIRST 7
declare @wk int
declare @yr int
declare @EndOfWeek as datetime
set @wk = 2
set @yr = 2010
SET @EndOfWeek = dateadd (week, @wk, dateadd (year, @yr-1900, 0)) + 1 - datepart(dw, dateadd (week, @wk, dateadd (year, @yr-1900, 0)) )
SELECT
replace('total for ' + convert(char(10), dateadd(day, -6, @EndOfWeek) , 110)
+ ' to ' + convert(char(10), @EndOfWeek, 110), '-', '/')
The result:
total for 01/04/2010 to 01/10/2010

- 86
- 4
I hope it helps:
declare @year char(4) = '2014'
declare @week int = 2
select dateadd(week,@week,convert(date,@year+'-01-01',121))
Change the date format appropriate for you from this list

- 1,270
- 1
- 10
- 21