Link to home
Avatar of Fordraiders
FordraidersFlag for United States of America

asked on

Access query to calculate time between 2 dates

Access query to calculate time between 2 date/time stamp
for sql server

What I need : to get the time elapsed hh:mm:ss between, 2 fields
[OPENED_AT],[inprogress_or_closed_at]



SELECT ID_Racfid, 
Seller_Name, 
Track_Name, 
Activity_Task, 
Notes, 
Closed_Date, 
NSC_ID_Ref, 
opened_at, 
inprogress_or_closed_at, 
DateDiff("h",[OPENED_AT],[inprogress_or_closed_at]) AS Hours, 
DateDiff("n",[OPENED_AT],[inprogress_or_closed_at]) AS Minutes, 
ElapsedTime([OPENED_AT],[inprogress_or_closed_at]) AS [Duration Elapsed], 
TotalTime([OPENED_AT],[inprogress_or_closed_at]) AS [Duration Total]
FROM DataEntry 

Open in new window



thanks
fordraiders
Avatar of ITguy565
ITguy565
Flag of United States of America image

https://support.office.com/en-us/article/calculate-the-number-of-days-between-two-dates-e73f4810-ea5b-4f82-86cd-93853f8029bd

this should be what you are looking for :

Right-click the form in the Navigation Pane and then click Layout View.
Click Design > Text Box, hold down the Ctrl key, and then click on the layout where you want to put the text box.
Click the new text box, and press F4 to display the Property Sheet if it's not already displayed.
On the Data tab of the Property Sheet, in the Control Source property box, type the following:
=DateDiff("d",Date(),[DueDate])
Avatar of Fordraiders

ASKER

Sorry, I need the query revised for sql server not Access
declare @StartDate datetime, @EndDate datetime

select @StartDate = '10/01/2012 08:40:18.000',@EndDate='10/04/2012 09:52:48.000'

select convert(varchar(5),DateDiff(s, @startDate, @EndDate)/3600)+':'+convert(varchar(5),DateDiff(s, @startDate, @EndDate)%3600/60)+':'+convert(varchar(5),(DateDiff(s, @startDate, @EndDate)%60)) as [hh:mm:ss]
Avatar of Bill Prew
Bill Prew

In SQL Server you can get the time difference between two datetime values by doing:

CONVERT(time, Date1-Date2)

Open in new window


»bp
SELECT [NSC_ID_Ref]
      ,[opened_at]
      ,[inprogress_or_closed_at]
 ,DateDiff(hh,[OPENED_AT],[inprogress_or_closed_at]) AS Hours
,DateDiff(mm,[OPENED_AT],[inprogress_or_closed_at]) AS Minutes
,DateDiff(ss,[OPENED_AT],[inprogress_or_closed_at]) AS Seconds

  FROM [flow].[dbo].time_entry]

this is working but getting an overflow error:

BUT YES, i NEED THE CALCULATION in ONE field
Bill,
Yours is returning:
opened_at                                 inprogress_or_closed_at             time                              should be
2016-02-09 07:33:23.000        2016-02-09 07:33:38.000             23:59:45.0000000        00:00:15
Not sure how you came up with the 55, but are subtracting the smaller from the larger?


»bp
sorry 15 sec
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
thanks all !!