Reply to comment

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

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <java>, <powershell>, <tsql>, <visualfoxpro>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.