We help IT Professionals succeed at work.

Finding gaps or missing dates in a date range using TSQL

Last Modified: 2017-03-20
I need to create a sql query very similar to the one listed in this article.  


However,  instead of the results which are listed below, I need for the empno (001) to have a record for each date, even though the results for timein/timeout will be NULL.  When I alter this query to retrieve records for all employees, I will need to have a date record for each date for each employee like below, not the results in the article.
logdate empno timein timeout
---------- ----- ------ -------
05/18/2015 001 08:30 16:30
05/19/2015 001 NULL NULL
05/20/2015 001 09:00 15:30
05/21/2015 001 NULL NULL
05/22/2015 001 08:30 17:30
Watch Question

PortletPaulEE Topic Advisor
Most Valuable Expert 2014
Awarded 2013

If you are doing this on a regular basis then you should seriously consider having a permanent "calendar table"
see: https://www.experts-exchange.com/articles/12267/SQL-Server-Calendar-Table.html

However it is possible to do this in many ways with or without such a table. The article uses a recursive CTE and while that approach works but isn't essential.
EE Topic Advisor
Most Valuable Expert 2014
Awarded 2013
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)


Thanks!  This was very helpful

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
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
Empower Your Career
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

Ask ANY Question

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

  • Troubleshooting
  • Research
  • Professional Opinions