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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:17:04+00:00 2026-05-31T17:17:04+00:00

Data: Here is sample table(TableA) data ID StartTime EndTime 1 2012-03-22 06:00:00.000 2012-03-22 06:30:00.000

  • 0

Data:
Here is sample table(TableA) data

ID  StartTime               EndTime
1   2012-03-22 06:00:00.000     2012-03-22 06:30:00.000
2   2012-03-22 06:15:00.000     2012-03-22 06:45:00.000
3   2012-03-22 06:30:00.000     2012-03-22 07:00:00.000
4   2012-03-22 06:45:00.000     2012-03-22 07:15:00.000
5   2012-03-22 07:00:00.000     2012-03-22 07:30:00.000
6   2012-03-22 07:15:00.000     2012-03-22 07:45:00.000
8   2012-03-22 07:30:00.000     2012-03-22 08:00:00.000
9   2012-03-22 07:45:00.000     2012-03-22 08:15:00.000
10  2012-03-22 08:00:00.000     2012-03-22 08:30:00.000
11  2012-03-22 08:15:00.000     2012-03-22 08:45:00.000
12  2012-03-22 08:30:00.000     2012-03-22 09:00:00.000
13  2012-03-22 08:45:00.000     2012-03-22 09:15:00.000
14  2012-03-22 09:00:00.000     2012-03-22 09:30:00.000
15  2012-03-22 09:15:00.000     2012-03-22 09:45:00.000
16  2012-03-22 09:30:00.000     2012-03-22 10:00:00.000

Requirement:
Get consecutive time sets for a given time range. e.g. time range:06:00 to 08:00



Expected Output:

ID  StartTime               EndTime
1   2012-03-22 06:00:00.000     2012-03-22 06:30:00.000
3   2012-03-22 06:30:00.000     2012-03-22 07:00:00.000
5   2012-03-22 07:00:00.000     2012-03-22 07:30:00.000
8   2012-03-22 07:30:00.000     2012-03-22 08:00:00.000

Question:
Is it possible to get the expected output using sql query? i don’t want to use loops.

i came up with this, but it only filter first non-consecutive row.

SELECT *
 FROM TableA TableA_OUTER (nolock) 
WHERE CONVERT(VARCHAR(5),EndTime,114) <= CONVERT(VARCHAR(5),CAST('08:00' AS DATETIME),114)
AND (CONVERT(VARCHAR(5),StartTime,114) = CONVERT(VARCHAR(5),CAST('06:00' AS DATETIME),114) OR EXISTS 
(SELECT NULL from TableA TableA_INNER (nolock) 
where  CONVERT(VARCHAR(5),TableA_OUTER.StartTime,114) = CONVERT(VARCHAR(5),TableA_INNER.EndTime,114)
))

Really appreciate your help!

  • 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-31T17:17:06+00:00Added an answer on May 31, 2026 at 5:17 pm

    Try this

    -- Create table
    
    CREATE TABLE [dbo].[TableA](
        [ID] [int] NULL,
        [StartTime] [datetime] NULL,
        [EndTime] [datetime] NULL
    ) ON [PRIMARY]
    
    GO
    
    --insert sample data
    Insert Into TableA (ID,StartTime,EndTime) values(1   ,'2012-03-22 06:00:00.000',     '2012-03-22 06:30:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(2   ,'2012-03-22 06:15:00.000',     '2012-03-22 06:45:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(3   ,'2012-03-22 06:30:00.000',     '2012-03-22 07:00:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(4   ,'2012-03-22 06:45:00.000',     '2012-03-22 07:15:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(5   ,'2012-03-22 07:00:00.000',     '2012-03-22 07:30:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(6   ,'2012-03-22 07:15:00.000',     '2012-03-22 07:45:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(8   ,'2012-03-22 07:30:00.000',     '2012-03-22 08:00:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(9   ,'2012-03-22 07:45:00.000',     '2012-03-22 08:15:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(10  ,'2012-03-22 08:00:00.000',     '2012-03-22 08:30:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(11  ,'2012-03-22 08:15:00.000',     '2012-03-22 08:45:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(12  ,'2012-03-22 08:30:00.000',     '2012-03-22 09:00:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(13  ,'2012-03-22 08:45:00.000',     '2012-03-22 09:15:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(14  ,'2012-03-22 09:00:00.000',     '2012-03-22 09:30:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(15  ,'2012-03-22 09:15:00.000',     '2012-03-22 09:45:00.000' )
    Insert Into TableA (ID,StartTime,EndTime) values(16  ,'2012-03-22 09:30:00.000',     '2012-03-22 10:00:00.000' )
    
     --query
    
    Declare @AnchorElement int
    Set @AnchorElement = (Select top 1 ID  from TableA 
    where '2012-03-22 06:00:00.000' 
    between StartTime and EndTime
    Order by StartTime
    )
    ;
    
    
     With ListStartingAtDate (ID,  StartTime, EndTime ,Level ) as
        (
            -- Anchor Timeframe definition
            select ID,  StartTime, EndTime , 0 as Level From TableA 
                --Where ID = @AnchorElement
                -- StartTime has to be matched exactly
                WHERE StartTime = '2012-03-22 06:00:00.000' 
            UNION ALL 
            -- Recursive Timeframe definition
            select a.ID,  a.StartTime, a.EndTime ,  Level +1 
            From TableA a
            INNER JOIN ListStartingAtDate b
            ON a.StartTime = b.EndTime
    
        )
    
        select  * from ListStartingAtDate
        where StartTime >= '2012-03-22 06:00:00.000' and EndTime <= '2012-03-22 08:00:00.000'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table named x Here is the sample data in table x Column1_
Here is sample data <table class=sparql border=1> <tr> <th>abstract</th></tr> <tr> <td> Cologne is Germany&#39;s
Here is the sample data for test table I have [childId] [parentId] [present] 1
Here is a sample code to retrieve data from a database using the yield
My html is like this <div id=rsContainer> <table id=rsTable><!-- DATA HERE --></table> </div> and
last time I asked how to populate a data structure here . Now I
Here a sample dataset: OrderProduct is a table that contains the productIds that were
I try to show my table data type in datagridview vb.net. Here is the
Here is a simple form I am using to send XML data in admin_xml.php
I'm having difficulty with pages that does not show the updated data immediately Here's

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.