• Jan Van der Eecken (5/7/2009)


    Apart from the formatting issue, what does it really mean if one adds two dates together?

    Nothing. The explanation is in fact misleading, since that is not what is happening here.

    If you check the "addiciton" topic in Books Online, you'll see that it mainly describes addition of two numbers, but also mentions the possibility of adding a number (in days) to a date. So this means that there are two alllowed forms of addition:

    1. number + number

    2. datetime + number

    The form used in this QotD: datetime + datetime is not supported - but it can be made into a supported form by first implictly converting the second datetime expressions to a numeric expression.

    Note that this is just an explanation and in no way an attempt to condone this behaviour. Even though the datetime + number form of addition is documented, I would never use it in production code as I consider it a gruesome hack. Please, everybody, use DATEADD(day, number, datetime) instead.

    Oh, and I also never rely on the current behaviour of implicit conversion between datetimes and numerics. (shudder)

    I was under the impression that converting a negative integer to a datetime would cause some kind of conversion error.

    If will if you use smalldatetime instead of datetime... :w00t:


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/