I have a T-SQL stored procedure where I want to search for a particular value and optionally limit the search to particular dates if they are passed in. If null values are passed in for either of these dates, then I want to ignore those. The way I am thinking of doing this by setting the input dates to minimum or maximum if they are null. I would prefer not to hardcode the minimum and maximum values though. So I am wondering what the SQL equivalent of the C# DateTime.MaxValue and DateTime.MinValue are.
I am thinking of using Coalesce like so
SELECT EmployeeName FROM Employee
WHERE EmployeeID = @EmployeeId AND
Birthday BETWEEN Coalesce(@StartDate, <MinDateTime>) AND
Coalesce(@EndDate, <MaxDateTime>)
Is there a built in function/constant/variable/enum I can use for the <MinDateTime> and <MaxDateTime> variables?
Any suggestions?
There’s no such functionality in SQL Server. You can easily find the min and max dates allowed in BOL (1753-01-01 – 9999-12-31). Or you could hard code another date easily (if you really are working with birthdays, 1800-01-01 – 2100-12-31 would probably suffice). Or you could (if it is the range query you’ve shown), have the coalesce fall back to the birthday itself:
But note that this will not necessarily scale well for very large tables.
Edited after accept, to respond to comment from OP
Generally, for SQL, if you’re needing “reference” data frequently, you add it as a table yourself. (Google for “calendar table” or “number table sql”). So in this instance, if you wanted to, you could add a “constants” (or maybe “limits” table):
Which you could then reference in queries (either through a subselect, or by cross joining to this table). E.g.
(The Lock, Primary Key, and Check constraint, work together to ensure that only a single row will ever exist in this table)