We help IT Professionals succeed at work.
Get Started

Help needed creating a union query in SQL

bfuchs asked
Last Modified: 2015-10-29
Hi Experts,

I have the following fields in PatientsEmployeesSchedule table

ID (AutoNum)
EmployeeID (int)
PatientID (int)
Day (DateTime) containing a date
From (DateTime) containing time
To (DateTime) containing time

(I know the name of fields are not the best as they are reserved words, but I cant change them at this point:(

Now the table contains scheduling records that usually starts & ends at the same day, however in case it does start at one day and finishes the next day (for example from 8PM to 8AM), only one record is being entered in the table with the day field specifying the first day meaning the day schedule started.

Now my question is, how can I have a query/view that will split that in two records in the most efficient way?

So if the table has the following:
ID          EmployeeID    PatientID     Day                From           To
1           123                  456              10/27/15        8:00 PM       8:00 AM

I would like to see the following:
ID          EmployeeID    PatientID     Day                From           To
1           123                  456              10/27/15        8:00 PM       12:00 AM
1           123                  456              10/28/15        12:00 PM     08:00 AM

Watch Question
Senior DBA
Most Valuable Expert 2018
Distinguished Expert 2019
This problem has been solved!
Unlock 4 Answers and 28 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE