Solved

Format Time Difference

Posted on 2008-06-10
6
209 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
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…

735 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