Solved

datediff format

Posted on 2000-03-30
2
381 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
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 100 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now