• Luis Cazares (8/27/2015)


    patrickmcginnis59 10839 (8/27/2015)


    drew.allen (5/30/2012)


    dwain.c (5/29/2012)


    Datetime functions do have their issues though, like the following:

    SELECT DATEADD(month, 1, '2012-02-29')

    Depending on your point of view, the results this returns looks wrong, or maybe not. I think they're not what I'd expect.

    Do you expect DATEADD(month, 1, '2012-02-01') to return '2012-03-03'? I certainly don't.

    How about '2012-02-15' returning '2012-03-17'? Not me.

    Or what about '2012-02-27' returning '2012-03-30'? Still no.

    Why would '2012-02-29' return anything other than '2012-03-29'?

    Why should the end of the month be treated any differently from any other day in the month? Or do you believe that the transition from counting from the beginning to counting from the end should happen at an earlier point in the month?

    Drew

    select dateadd(month,1,'2012-01-31')

    LOL

    I think the best plan is to know what dateadd does with this 'month' parameter, its not hard to test all cases.

    That's only because February doesn't have 31 days and it's changed to the last day available for that month.

    There's no way to ensure that February 28th is used as the last day of the month or just the 28th day.

    I'm thinking probably the closest rule that would make sense is to add one to the "month" value then decrement the resulting day enough to make the result a valid date. However, I wouldn't venture to use that rule without testing it.

    edit: make sense to ME. others results may vary 🙂