Week number in a Month

This is sample code. Add error handling and adjust to your requirements as necessary.

-- There's simpler and shorter version in the comments below
 
DECLARE @dt DATETIME, @WeekOfMonth TINYINT
SET @dt = '2007-07-08'
 
SET @WeekOfMonth = (DAY(@dt) + 
				(DATEPART(dw, DATEADD (MONTH, DATEDIFF (MONTH, 0, @dt), 0)) 
				  --^-- The day of the week for the first day of month
			-1) -- # of days to add to make the first week full 7 days
		-1)/7 + 1 
PRINT @WeekOfMonth
Your rating: None Average: 3.5 (4 votes)

Another way

Try this

DECLARE @dt DATETIME, @WeekOfMonth TINYINT
SET @dt = '2007-07-08'
SET @WeekOfMonth = DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH, 0, @dt), 0), @dt) +1
PRINT @WeekOfMonth

Shorter and simpler

It's shorter and simpler.

Thanks.

Very Cool. Thanks for posting

Very Cool.
Thanks for posting this...

Re: Very Cool. Thanks for posting

Really Helpful. Thank You

Re: Another way

Great.... Just the thing I was looking for.
Thanks...

wow..cool......thanx lot...

wow..cool......thanx lot...

One use for this week of the month:

-- parts of this taken from http://joecasella.wordpress.com/2011/05/10/tsql-tuesday-18-using-a-recursive-cte-to-create-a-calendar-table/#comment-38
CREATE PROCEDURE dbo.spGetMonthCalendar
	@MONTH INT = 1,
	@YEAR INT = 2010
AS BEGIN
 
	DECLARE @StartDate DATE
	DECLARE @EndDate DATE
 
	-- Create the start date value
	SET @StartDate = CONVERT(DATE, RIGHT('0000' + CONVERT(VARCHAR, @YEAR),4) + '-' + RIGHT('00' + CONVERT(VARCHAR, @MONTH), 2) + '-01')
	-- Create the end date
	SET @EndDate = DATEADD(M,1,@StartDate)
 
	;WITH Sales_CTE (MonthDate)
	AS
	(
	SELECT 
		@StartDate
	UNION ALL
	SELECT DATEADD(dd, 1, MonthDate) AS MonthDate 
		FROM Sales_CTE 
	WHERE 
		DATEADD(dd, 1, MonthDate) < @EndDate
	)
	SELECT 
		[1] AS sun, 
		[2] AS mon, 
		[3] AS tue, 
		[4] AS wed, 
		[5] AS thu, 
		[6] AS fri, 
		[7] AS sat
	FROM
		(
		SELECT 
			DAY(MonthDate) AS MONTHDATE,
			DATEPART(weekday, MonthDate) AS DAYOFWEEK,
			DATEDIFF(week, DATEADD(MONTH, DATEDIFF(MONTH, 0, MonthDate), 0), MonthDate) +1 AS ROW
			--row_number() over(order by MonthDate)/7 as ROW 
		FROM Sales_CTE 
		) AS source_table
	pivot (
		SUM(monthdate)
		FOR dayofweek in ([1],[2],[3],[4],[5],[6],[7])
		) AS pivottable
END

The results (if ran with default parameters):

sun mon tue wed thu fri sat
NULL NULL NULL NULL NULL 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 NULL NULL NULL NULL NULL NULL

Agreed Very Cool!

Agreed Very Cool!

will it works for the date

will it work for the date 1/31/2010 ?
if so it will be the 6th week for that month.

Yes it's correct

Yes it's correct. The 1/31/2010 is Sunday of the 6th week in the month.

Thanks for the quick

Thanks for the quick response.
Then it wont work for me, i need it as a 6th week, So please can you give me a query to make it as 5th week like
jan 1st ---7th 1st week
jan 8th -- 14th 2nd week
jan 15th--21nd 3rd week
jan 22rd--28th 4th week
jan 29th -- what ever comes hear as 5th week
and so on.

I mean jan 1st of 2010 to be as first day to calculate the week number.

It's simple

It's simple

SELECT ((DATEPART(DAY, @dt)-1) / 7) + 1

Thank you very very much

Thank you very very much sergey

Sergey! perfect solution!

Sergey! perfect solution! I've been looking for this information for 2 days! Thanks again.

Calculate first and last day of a week in a month

Hi Sergey ,

I want to calculate first day and last day of a week in a month.For Example,

sep 01 2011 - sep 03 2011,
sep 04 2011 - sep 10 2011,
sep 11 2011 - sep 17 2011 ,
sep 18 2011 - sep 24 2011,
sep 25 2011 - sep 30 2011,