Link to home
Create AccountLog in
Avatar of Jason Yousef
Jason YousefFlag for United States of America

asked on

Get the last 13 week from sql query

Hello,
I used this query before going to 2011 but now it's not working and can't get it to work!

where   [Week] <= (select DATEPART(Wk, GETDATE()))
  and
   [Week] > (select DATEPART(Wk, GETDATE())-13)

how to get the last 13 weeks' numbers?

Thanks



Avatar of chapmandew
chapmandew
Flag of United States of America image

datepart(week, datefield)
ASKER CERTIFIED SOLUTION
Avatar of Wouter Boevink
Wouter Boevink
Flag of Netherlands image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
where  
[Week] <= (select DATEPART(Wk, GETDATE()))
  and
[Week] > datepart(week,dateadd(week, -13, getdate()))
@Erick37 this won't work

where  
[Week] <= (select DATEPART(Wk, GETDATE()))
  and
[Week] > datepart(week,dateadd(week, -13, getdate()))

Results in

where week <= 2 and week > 41

And that's impossible
The WHERE Caluse you mention is correct to my knowledge, Now the week start from 1 for the year 2011. T-SQL cannot add up week number with the last year. 2010 year's week ends as 53.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Jason Yousef

ASKER

Thanks all for your help, the solution didn't include the year, so I had to implement it.