January 7, 2010 at 5:25 pm
If anyone would be kind enough to review the below code that would be much appreciated. I'm looking for any flaws in my logic that would produce incorrect results given the descriptions of my three scenarios.
DECLARE @Scenario int
--Scenario 1
--It is a friday that is not the first friday of the month
--Scenario 2
--It is a friday that is the first friday of the month
--Scenario 3
--It is a friday that is the first friday of the month and it is also the first day of the month
DECLARE @DayOfWeek int --numerical representation of the day of the week
SET @DayOfWeek = DATEPART(dw,getdate())
DECLARE @DayOfMonth int --numerical representation of the day of the month
SET @DayOfMonth = DATEPART(day,getdate())
IF @DayOfWeek = 6 and @DayOfMonth > 7
BEGIN
SET @Scenario = 1
END
IF @DayOfWeek = 6 and @DayOfMonth <= 7
BEGIN
SET @Scenario = 2
END
IF @DayOfWeek = 6 and @DayOfMonth = 1
BEGIN
SET @Scenario = 3
END
PRINT getdate()
PRINT @DayOfWeek
PRINT @DayOfMonth
IF @Scenario IS NOT NULL
BEGIN
PRINT @Scenario
END
ELSE
BEGIN
PRINT 'It is not scenario 1, 2, or 3'
END
January 7, 2010 at 6:19 pm
Review your code against this excellent blog post
https://www.sqlservercentral.com/blogs/lynnpettis/archive/2009/03/25/some-common-date-routines.aspx
and see if it matches what you have posted or gives you a different way of doing the same calculation.
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply