Date based logic

  • 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

  • Review your code against this excellent blog post

    https://qa.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.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply