Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calculating hours and minutes between dates, and if the span of time overlaps two months

Posted on 2013-06-27
7
Medium Priority
?
605 Views
Last Modified: 2013-06-27
I have a query with a begin and end date parameter.  However, I find I also need to select people who have time in a house that overlaps from one month to the next.  They don't show because of the end date.

Example:  Parameters - 01/01/2013 to 01/31/2013
 Person1 - At a facility during the month
Person2 - At a facility 01/25/2013 to 02/05/2013
Person3 - At a facility 12/28/2012 to 01/04/2013

Person1 will show up, person2 won't because the end date isn't <= @enddate
Person3 won't show because of the begindate.

I also calculate how many days and hours they are at a facility.  I want to capture the overlap people and only show the days and hours for the time during the begin and end dates.  I've attached the part of my code with the calculations and selections.
SQLQuery6.sql
0
Comment
Question by:Sherry
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39281911
sample data & expected results?
this will certainly help provide a speedy result
0
 
LVL 8

Accepted Solution

by:
didnthaveaname earned 1500 total points
ID: 39281917
Why don't you check to see if:

( m1.prmvt_mvmt_ts >= @beginDate or m2.prmvt_mvmt_ts <= @endDate ) and
datediff( mm, m1.prmvt_mvmt_ts, m2.prmvt_mvmt_ts ) >= 1

Edit: congrats on getting 1mil points, Paul!  I am going to hold off on the case statement until you're able to post some sample data/examples because there are a lot of different ways that can go, whereas, I think the previous was what you were looking for for that part...
0
 

Author Closing Comment

by:Sherry
ID: 39281932
Thank you.  That was about the only one I hadn't tried.
0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 49

Expert Comment

by:PortletPaul
ID: 39281991
@didnthaveaname - :) thanks, seemed like those last few points would never arrive
Cheers, Paul.
0
 

Author Comment

by:Sherry
ID: 39281995
I'll get some posted.  I see that I'm getting most of what I want, but I'm also getting some that I shouldn't.  I'm wondering if maybe an IF statement in my where clause might work better.
0
 

Author Comment

by:Sherry
ID: 39282020
Full Query and data results.  I removed the columns that are irrelevant.
SQLQuery1.sql
SampleData.xlsx
0
 
LVL 8

Expert Comment

by:didnthaveaname
ID: 39282068
I think this should do it:

   case
      when M.RELEASE_DATE > @end_Date then ( dateDiff( hour, M.confinement_date, @endDate ) / 24 )
      when M.CONFINEMENT_DATE < @begin_date then ( dateDiff( hour, @begin_date, M.release_date ) / 24 ) as TotalDays
      else null
   end as TotalDays,
   case
      when M.RELEASE_DATE > @end_Date then ( dateDiff( hour, M.confinement_date, @endDate ) % 24 )
      when M.CONFINEMENT_DATE < @begin_date then ( dateDiff( hour, @begin_date, M.release_date ) % 24 ) as TotalDays
      else null
   end as LeftOverHours,

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question