Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1072757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:54:17+00:00 2026-05-16T20:54:17+00:00

I have a T-SQL stored procedure where I want to search for a particular

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:54:17+00:00Added an answer on May 16, 2026 at 8:54 pm

    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:

    SELECT EmployeeName FROM Employee
    WHERE  EmployeeID = @EmployeeId AND
    Birthday BETWEEN Coalesce(@StartDate, Birthday) AND  
                     Coalesce(@EndDate, Birthday)
    

    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):

    create table Constants (
        Lock char(1) not null,
        datetimeMin datetime not null,
        datetimeMax datetime not null,
        intMin int not null,
        intMax int not null,
        /* Other Min/Max columns, as required */
        constraint PK_Constants PRIMARY KEY (Lock),
        constraint CK_Constants_Locked CHECK (Lock='X')
    )
    insert into Constants (Lock,datetimeMin,datetimeMax,intMin,intMax)
    select 'X','17530101','99991231',-2147483648,2147483647
    

    Which you could then reference in queries (either through a subselect, or by cross joining to this table). E.g.

    SELECT EmployeeName
    FROM Employee, Constants
    WHERE  EmployeeID = @EmployeeId AND
    Birthday BETWEEN Coalesce(@StartDate, Constants.datetimeMin) AND  
                     Coalesce(@EndDate, Constants.datetimeMax)
    

    (The Lock, Primary Key, and Check constraint, work together to ensure that only a single row will ever exist in this table)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a PL/SQL stored procedure as follows: PROCEDURE do_something(foo VARCHAR2 DEFAULT NULL)
I have sql stored procedure which pass and return some values: CREATE PROCEDURE [dbo].[GetMoney]
I have a PL/SQL stored procedure inside which I want to run several updates,
I have a Microsoft SQL stored procedure whose column name I want to set
We have a page calling a Search stored procedure which internally uses SQL Server
I have a SQL Server stored procedure that is not returning any data when
I have a T-SQL stored procedure with the signature CREATE PROCEDURE MyProc @recordCount INT
I have a SQL server stored procedure which would accept date as input param
I have two PL/SQL Stored procedure each handling its own Transaction (Begin/Commit and Rollback
I have a huge pl/sql stored procedure, where we make some deletions as long

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.