SQL Server function to return minimum date (January 1, 1753)

前端 未结 8 644
生来不讨喜
生来不讨喜 2021-02-01 11:43

I am looking for a SQL Server function to return the minimum value for datetime, namely January 1, 1753. I\'d rather not hardcode that date value into my script.

Does an

8条回答
  •  粉色の甜心
    2021-02-01 12:12

    You could write a User Defined Function that returns the min date value like this:

    select cast(-53690 as datetime)
    

    Then use that function in your scripts, and if you ever need to change it, there is only one place to do that.

    Alternately, you could use this query if you prefer it for better readability:

    select cast('1753-1-1' as datetime)
    

    Example Function

    create function dbo.DateTimeMinValue()
    returns datetime as
    begin
        return (select cast(-53690 as datetime))
    end
    

    Usage

    select dbo.DateTimeMinValue() as DateTimeMinValue
    
    DateTimeMinValue
    -----------------------
    1753-01-01 00:00:00.000
    

提交回复
热议问题