SvenB
SvenB

Reputation: 139

T-SQL calculate with calendar week

I have a T-SQL question - I'm using SQL-Server 2012:

I have a varchar calendar week column (1-52). In my where clause I only want to select the last 6 weeks going back from the current calendar week of the current year. I also have a year varchar column with values of "2011" , "2012" and "2013".

But now I'm struggling to set the expression for the where clause.

Or do I need a Having clause to get this done?

I've tried it like this:

Having Calendarweek BETWEEN max(Calendarweek)-6 AND max(Calendarweek) AND Year=2013

but it returns all weeks.

Hope you can help me out with this. thank you.

Upvotes: 0

Views: 767

Answers (1)

www
www

Reputation: 4391

Try this

where Year ='2013'
and CAST(Calendarweek AS INT) >= datepart(week,getdate())-6 

Upvotes: 1

Related Questions