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 6097193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:00:53+00:00 2026-05-23T13:00:53+00:00

How do I write a T-SQL query where the filter conditions of datetime ranges

  • 0

How do I write a T-SQL query where the filter conditions of datetime ranges are generated from another query.

For Example Query A would Return:

StartTime                   EndTime
2011-07-06 04:05:42.137     2011-07-06 04:05:58.503
2011-07-06 04:25:51.103     2011-07-06 04:26:07.017
2011-07-06 04:55:56.240     2011-07-06 04:56:04.480
...

How would I return all results that are within the starttime and endtime pairs?

  • 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-23T13:00:53+00:00Added an answer on May 23, 2026 at 1:00 pm

    This WHERE clause will return all records that overlap with a specific window of time.

    WHERE
          StartTime < myEndTimeLimit
      AND EndTime   > myStartTimeLimit
    

    Note: Depending on whether your endTimes are inclusive (up to and Including that moment) or excluse (Everything Before that moment) you may need <= and >= rather than < and >.

    Equally, this can be part of a join…

      TableA
    INNER JOIN
      TableB
        ON  <some matching condition>
        AND TableA.StartTime < TableB.EndTime
        AND TableA.EndTime   > TableB.StartTime
    

    (TableB could be a sub-query)

    Note: This type of query is Exceptionally poor in terms of optimisation with an Index.

    If you have a year’s worth of data, and you look for an overlap with “today”, the first condition (A.Start < B.End), nearly all of the records are returned, and then scanned to match with (A.End > B.Start).

    The simplest optimisation is to “know” the maximum length of any window of time. If all entries are ALWAYS less than 30 days, you could do the following (Amend for your RDBMS’s notation).

      TableA
    INNER JOIN
      TableB
        ON  <some matching condition>
        AND TableA.StartTime <  TableB.EndTime
        AND TableA.StartTime >= TableB.StartTime - 30
        AND TableA.EndTime   >  TableB.StartTime
    

    This now gives a 30 day range in which TableA.StartTime could exist, giving much better performance over time.

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

Sidebar

Related Questions

Is it possible to write sql query (in mssql), that returns decimals from given
I would like to write SQL query for finding the node ID (nid) for
Is it possible to write a PL/SQL query to identify a complete list of
Can anyone tell me how to write a nested SQL query like SELECT *
I am trying to write a query for SQL Server 2005 but I can't
In SQL one can write a query that searches for a name of a
Im very new to SQL but need to write a query to do the
I am trying write a SQL query that filters a gridview by the fields
Need to write a SQL query to search special character in a column. Text
If I write a sql query for mysql and I just specify JOIN (no

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.