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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:21:11+00:00 2026-05-25T23:21:11+00:00

I’m surprised this hasn’t come up yet. In T-SQL, I need to find the

  • 0

I’m surprised this hasn’t come up yet.

In T-SQL, I need to find the intervals (defined by startDateTime and endDateTime) that overlap with daily interval (say 9am-5pm).

For example, with table:

CREATE TABLE [dbo].[Interval](
    [startDateTime] [datetime] NOT NULL,
    [endDateTime] [datetime] NOT NULL
)

Solution would be the procedure that returns only overlapping intervals:

CREATE PROCEDURE FindIntervals
    -- Add the parameters for the stored procedure here
    @from varchar(5) = '9:00', 
    @to varchar(5) = '17:00'
AS
BEGIN
    select * from Interval
    where ...
END
GO

EDIT:
Example intervals:

  1. Sep 7 2011 8:00 AM – Sep 7 2011 8:30 PM
  2. Sep 7 2011 11:00 AM – Sep 7 2011 1:00 PM
  3. Sep 7 2011 1:00 PM – Sep 7 2011 6:00 PM
  4. Sep 9 2011 8:00 AM – Sep 9 2011 8:30 PM
  5. Sep 9 2011 11:00 AM – Sep 9 2011 1:00 PM
  6. Sep 9 2011 1:00 PM – Sep 9 2011 6:00 PM

So, for given interval “nine to five”, 2, 3, 5 and 6 should be returned, as they overlap the given input.

But,

  1. Sep 9 2011 8:00 AM – Sep 10 2011 8:30 PM

also fits, because it includes entire day.

Please, I need help with matching string and datetime values in T-SQL, not abstract “less then”/”greater then” solutions.

  • 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-25T23:21:12+00:00Added an answer on May 25, 2026 at 11:21 pm
    declare @Interval table
    (
      startDateTime datetime,
      endDateTime datetime
    )
    
    insert into @Interval values
    ('2011-09-07T08:00:00', '2011-09-07T08:30:00'),
    ('2011-09-07T11:00:00', '2011-09-07T13:00:00'),
    ('2011-09-07T13:00:00', '2011-09-07T18:00:00'),
    ('2011-09-09T08:00:00', '2011-09-09T08:30:00'),
    ('2011-09-09T11:00:00', '2011-09-09T13:00:00'),
    ('2011-09-09T13:00:00', '2011-09-09T18:00:00'),
    ('2011-09-09T08:00:00', '2011-09-10T08:30:00')
    
    declare @from varchar(5) = '09:00'
    declare @to varchar(5) = '17:00'
    
    ;with L(MinDate, MaxDate) as
    (
      select dateadd(day, datediff(day, 0, min(startDateTime)), 0),
             dateadd(day, datediff(day, 0, max(endDateTime)), 0)
      from @Interval
    ), 
    D(fromTime, endTime) as
    (
      select dateadd(day, Number.number, L.MinDate)+cast(@from as datetime),
             dateadd(day, Number.number, L.MinDate)+cast(@to as datetime)
      from L
        inner join master..spt_values as Number
          on Number.number <= datediff(day, L.MinDate, L.MaxDate)
      where Number.type = 'P'
    )
    select I.startDateTime,
           I.endDateTime
    from @Interval as I
    where exists (select *
                  from D
                  where I.startDateTime < D.endTime and
                        I.endDateTime > D.fromTime)
    

    Result:

    startDateTime           endDateTime
    ----------------------- -----------------------
    2011-09-07 11:00:00.000 2011-09-07 13:00:00.000
    2011-09-07 13:00:00.000 2011-09-07 18:00:00.000
    2011-09-09 11:00:00.000 2011-09-09 13:00:00.000
    2011-09-09 13:00:00.000 2011-09-09 18:00:00.000
    2011-09-09 08:00:00.000 2011-09-10 08:30:00.000
    

    If you expect to have a date range of more than 2048 days you need to replace master..spt_values with a numbers table. Make sure the numbers table starts with 0.

    SQL Server 2008 version

    ;with L(MinDate, MaxDate) as
    (
      select cast(min(startDateTime) as date),
             cast(max(endDateTime) as date)
      from @Interval
    ), 
    D(fromTime, endTime) as
    (
      select dateadd(day, Number.number, L.MinDate)+cast(@from as datetime),
             dateadd(day, Number.number, L.MinDate)+cast(@to as datetime)
      from L
        inner join master..spt_values as Number
          on Number.number <= datediff(day, L.MinDate, L.MaxDate)
      where Number.type = 'P'
    )
    select I.startDateTime,
           I.endDateTime
    from @Interval as I
    where exists (select *
                  from D
                  where I.startDateTime < D.endTime and
                        I.endDateTime > D.fromTime)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.