views:

93

answers:

3

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 anything like that exist? (For comparison, in C#, I could just do DateTime.MinValue) Or would I have to write this myself?

I am using Microsoft SQL Server 2008 Express.

A: 

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.

RedFilter
I was hoping to avoid writing my own, but it looks like that is my only option.
Jeremy
If you're going to have an arbitrary constant, `'1753-1-1'` is much better than `-53690`
Gabe
@Gabe: Yes, I was thinking the same thing. Coming back to the script, I would have no idea what -53690 means.
Jeremy
The function name would make obvious the meaning of the value.
RedFilter
A: 

Have you seen the SqlDateTime object? use SqlDateTime.MinValue to get your minimum date (Jan 1 1973).

Naeem Sarfraz
This needs to be a SQL function, not a .NET function.
Jeremy
+1  A: 

The range for datetime will not change, as that would break backward compatibility. So you can hard code it.

Andomar
That is true, but it would still be nice to call GetMinDate() or something like that instead of CONVERT(datetime, '1753-1-1') wherever I needed to use it.
Jeremy
@Jeremy: I know what `convert` does, but for `GetMinDate()` I'd have to dive into the function definition. So if I had to maintain your code, I'd prefer the `convert` variant.
Andomar