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

x
?
Solved

datediff format

Posted on 2000-03-30
2
Medium Priority
?
395 Views
Last Modified: 2006-11-17
I want to return the difference between two dates in hours and minutes. I am using the following code to return hours:

datediff(hh,pd.starttime,pd.endtime) duration

can anyone tell me how to get minutes as well.

Thanks
0
Comment
Question by:cb1
[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
2 Comments
 
LVL 1

Expert Comment

by:drom
ID: 2669380
short answer:
DATEDIFF(mi,pd.starttime,pd.endtime)

a bit larger:
select STR(DATEDIFF(hh,pd.starttime,pd.endtime)) + ":" + STR(DATEDIFF(mi,pd.starttime,pd.endtime) - DATEDIFF(hh,pd.starttime,pd.endtime)*60, 2)
from tablename pd

will give you time difference in string in hhh:mm format (one dowside - it's space instead of leading '0' in mm)

Regards
Dmitry.
0
 
LVL 4

Accepted Solution

by:
Gustavo Perez Buenrostro earned 400 total points
ID: 2670912
cb1,
Next code lets you retrieve the result you want (using MS-SQL Time Format):

declare @dtStartTime datetime
       ,@dtEndTime datetime

select @dtStartTime='20000301 15:23:55.157'
      ,@dtEndTime='20000301 16:24:56.158'

select left(convert(varchar,dateadd(ms,datediff (ms,@dtStartTime,@dtEndTime),0),108),5)



Here is the result:

-----
01:01
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

721 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