finding max possible date in ms sql server 2005+

前端 未结 7 1234
名媛妹妹
名媛妹妹 2021-01-01 08:22

Is there a function like GETDATE() in Sql Server 2005 that let\'s you get the max possible date?

I do not want to find the highest date in a table. I wa

7条回答
  •  清歌不尽
    2021-01-01 09:20

    CAST() seems to be dependent on the SQL Server language/culture.

    On my German SQL Servers 2008 R2 and 2012 (@@language = 'Deutsch'), the following cast throws an error:

    CAST('12/31/9999 23:59:59.997' AS DATETIME)
    

    The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

    Whereas this one works just fine:

    CAST('31.12.9999 23:59:59.997' AS DATETIME)
    

    SOLUTION

    I think the safest approach is to specify the format with CONVERT():

    /* ISO 8601 */
    CONVERT(DATETIME, '9999-12-31T23:59:59.997', 126)
    

提交回复
热议问题