Home Forums SQL Server 2005 T-SQL (SS2K5) How to find the 2nd Monday following a given date. RE: How to find the 2nd Monday following a given date.

  • SwePeso (8/11/2013)


    Remember that SET DATEFIRST will recompile your code. And also, not everyone has DATEFIRST 1 as default setting.

    Try this code that is safe and doesn't rely on any setting and also, if doesn't have the annoying time portion.

    SELECTGETDATE() AS Today,

    DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()) / 7 * 7, '19000108') AS NextMonday,

    DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()) / 7 * 7, '19000115') AS MondayAfterNext

    Isn't this more or less the same thing with a little less math?

    SELECT Today=GETDATE()

    ,NextMonday=DATEADD(week, 1+DATEDIFF(week, 0, GETDATE()), 0)

    ,MondayAfterNext=DATEADD(week, 2+DATEDIFF(week, 0, GETDATE()), 0)


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St