in

jetlounge

the minty fresh blog that whitens your teeth!


Syndication


T-SQL: Get the Week Number for a Given Month

by David Leibowitz | December 11, 2007 | Comments: 1
This function is fairly simple. Pass in a date and we return the Week # of the month. Generally this is 1 -5. In a rare case, like April 2006, this may stretch to 6. It can be customized for custom calendars.

declare @p_date smalldatetime
set @p_date = '5/15/2007' 
--or any date you like

select datediff(wk, cast(cast(datepart(mm,@p_date) as nvarchar(2)) + '/01/' +  cast(datepart(yyyy,@p_date) as nvarchar(4)) as smalldatetime),@p_date) +1

In this case, the result would be 3.

del.ico.us digg this technorati BlinkList Furl reddit DotNetKicks google! live Facebook Stumble Upon Yahoo!




Comments

 

David said:

Cool.  Will give it a try.  Thanks.

May 26, 2009 5:29 PM

Leave a Comment

(required)  
(optional)
(required)  
Add
Powered by Community Server (Non-Commercial Edition), by Telligent Systems