Solved

Format Time Difference

Posted on 2008-06-10
6
210 Views
Last Modified: 2010-04-21
What is the best way to return the time difference in nn:hh format?

Is there a better way to write this statement?

SELECT DATEDIFF("n",#1:13:44#,#2:45:33#) & FORMAT(#2:45:33# - #1:13:44#,":ss")

Note that this will not work:
SELECT FORMAT(#2:45:33# - #1:13:44#,":ss")
because it will not return a value greater than 60 minutes.
0
Comment
Question by:Thomasian
[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
  • 3
6 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 21749608
I don't really know what you mean by ' nn:hh format'.

If you want the time difference in hms then you can do:

FORMAT(#2:45:33# - #1:13:44#,"hh:mm:ss")
0
 
LVL 22

Author Comment

by:Thomasian
ID: 21750387
Most of the difference will be less than 1 hour. If I use "hh:nn:ss", most of the values (if not all) returned will have a value 0. That is why I want to combine the hour and minute value.

e.g.
A difference of 1:30:12 will be returned as 90:12
0
 
LVL 77

Expert Comment

by:peter57r
ID: 21750467
You cannot show an minute value greater than 59. It would produce an invalid time value.

If you want to show the result like that you will have to do some calculations and create a string to display the value.

cstr(DATEDIFF("s",#1:13:44#,#2:45:33#) \60 ) & ":" & cstr(DATEDIFF("s",#1:13:44#,#2:45:33#)  mod 60 )
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 22

Author Comment

by:Thomasian
ID: 21750492
Will that be better than the query I posted?

SELECT DATEDIFF("n",#1:13:44#,#2:45:33#) & FORMAT(#2:45:33# - #1:13:44#,":ss")
0
 
LVL 77

Accepted Solution

by:
peter57r earned 250 total points
ID: 21750956
yes.
Datediff ("n",.......) will just look at the minute numbers so half the time you will get the wrong answer

DATEDIFF("n",#1:13:59#,#1:14:00#) gives 1 (minute)
0
 
LVL 22

Author Closing Comment

by:Thomasian
ID: 31465660
Thanks, I didn't notice that.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

740 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