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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:31:26+00:00 2026-05-30T23:31:26+00:00

I am working on a project in a company. I am trying to solve

  • 0

I am working on a project in a company. I am trying to solve this query but I could not.

My tables are:

  1. Appointments:

    doctorId  patientId  patientName  fromDateTime           toDateTime
    --------  ---------  -----------  ---------------------  ---------------------
    56        1          fghfgh       3/23/2012 12:15:00 PM  3/23/2012 01:15:00 PM
    56        2          asdadf       3/23/2012 01:15:00 PM  3/23/2012 02:15:00 PM
    56        3          werwr        3/23/2012 09:15:00 AM  3/23/2012 10:15:00 AM
    57        4          uiyui        3/23/2012 09:15:00 AM  3/23/2012 10:15:00 AM
    57        5          asdad        3/23/2012 01:15:00 PM  3/23/2012 02:15:00 PM
    
  2. This is my timeSchedule table:

    id  startTime     endTime
    --  ------------  ------------
    1   08:00:00.000  09:00:00.000
    2   09:00:00.000  10:00:00.000
    3   11:00:00.000  12:00:00.000
    4   12:00:00.000  13:00:00.000
    5   13:00:00.000  14:00:00.000
    6   14:00:00.000  15:00:00.000
    7   15:00:00.000  16:00:00.000
    

    Actually there are more values but I think these are enough to solve the problem.

I am comparing patient appointments with this timeSchedule table.

Now suppose if I pass parameter doctorId as 56 and consider today is 23 March then output should be like this:

id  startTime  endTime
--  ---------  --------
1   08:00 AM   09:00 AM
3   11:00 AM   12:00 PM
6   02:00 PM   03:00 PM
7   03:00 PM   04:00 PM

How can I achieve the above result?

  • 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-30T23:31:27+00:00Added an answer on May 30, 2026 at 11:31 pm

    Assuming that timeSchedule.startTime and timeSchedule.endTime are both Time data types then it would be something like this…: (if not, you could cast as such).

    DECLARE @pDoctorID Int = 56
    DECLARE @pDate Date = '3/23/2012'
    
    SELECT * FROM timeSchedule WHERE 
    NOT Exists (
                    SELECT doctorid FROM Appointments 
                    WHERE doctorid = @pDoctorID AND 
                    CAST(fromDatetime as Date) = @pDate AND  
                    (
                      (CAST(fromDatetime as Time) >= timeSchedule.startTime AND
                      CAST(fromDatetime as Time) <= timeSchedule.endTime)
                      OR
                      (CAST(toDatetime as Time) >= timeSchedule.startTime AND
                      CAST(toDatetime as Time) <= timeSchedule.endTime)
                      OR
                      (CAST(toDatetime as Time) <= timeSchedule.startTime AND
                      CAST(fromDatetime as Time) >= timeSchedule.endTime)
                     )
                   )
    

    Which with your sample data returns this:

    1 | 08:00:00.00 | 09:00:00.00

    4 | 11:00:00.00 | 12:00:00.00

    8 | 15:00:00.00 | 16:00:00.00

    In essence the query is saying give me any appointment for this doctor where existing appoints do not start or end between the time frames, or start before and end after any of the time slots defined by the timeSchedule table.

    Formatting the return times is also a simple matter. See the table in this link for all your options.

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

Sidebar

Related Questions

I've been working with a company that, in this current project, has to implement
I've been working on this project for my company and my boss said: we
As a project for my company I am trying to query two ADs with
I'm working on a project for my company that uses a socket server (php)
Good afternoon, I'm working on a project for my company to run an update
I'm working on a fairly large project for a trading company in Philadelphia. The
I have a working project that Im amending, it crashes after trying to use
I have a project working fine under MSVS 2010 SP1. I'm trying to convert
I am working on a project for my company, and while tracing previously written
I am working on a project for my company, and I need to integrate

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.