Determine the current week in a month

It is quiet easy to determine the current week in a year as shown below :

SELECT DATEPART( wk, GETDATE())

However, to determine the current week in a month, use this query written by AMB

select datediff(week, convert(varchar(6), getdate(), 112) + '01', getdate()) + 1

returns 5 as the date today is 24 December 2007


About The Author

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook

6 comments:

Pavan said...

Dear Friend,

Its more helpful.I am very thankful for providing this valuable information.bye.....

Anonymous said...

thanks a lot helped a lot

Anonymous said...

GRACIAS!!!!!
Me ayudaste mucho con este dato.

Anonymous said...

This was extremely helpful! You saved my day!!!!

Anonymous said...

Hi ,

I tested out this SQL Statement and it doesn't seem to work. My week starts on a Monday, so I set SQLs DATEFIRST to 1.

I then ran the statement and declared the date as '2009-06-07'. When I ran the query it returned the value 2, which is incorrect. It should've returned 1, as the 7th June 2009 is still part of the first week that starts on a Monday and ends on a Sunday.

Any suggestions on how to get around this?

Anonymous said...

Try this instead
datediff(week, convert(varchar(6),@Date, 112) + '01', dateadd(dd,-1,@Date)) + 1