Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Calculating the time difference
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

12 Posts

Posted - 11/10/2006 :  07:11:20  Show Profile  Reply with Quote
We have this StartTime and EndTime (both datetime fields) and now we are calculating the difference in minutes beween two dates.Using datediff(mi,StartTime,EndTime) giving iccorect result e.g:-

SELECT datediff(mi,'2006-11-10 05:47:53.497','2006-11-10 05:48:10.420') gives 1 minute where as it is 17 seconds.What is the best way to calculate the difference in minutes?

Flowing Fount of Yak Knowledge

5581 Posts

Posted - 11/10/2006 :  07:16:10  Show Profile  Visit harsh_athalye's Homepage  Click to see harsh_athalye's MSN Messenger address  Send harsh_athalye a Yahoo! Message  Reply with Quote
That is because you are asking SQL to return difference in minutes...Use ss or s or second to return in seconds

SELECT datediff(ss,'2006-11-10 05:47:53.497','2006-11-10 05:48:10.420')

Or if you want minutes in fractions:

SELECT convert(decimal(5,2),datediff(ss,'2006-11-10 05:47:53.497','2006-11-10 05:48:10.498')/60.00)

Harsh Athalye
"Nothing is Impossible"

Edited by - harsh_athalye on 11/10/2006 07:18:49
Go to Top of Page


United Kingdom
22859 Posts

Posted - 11/10/2006 :  07:26:27  Show Profile  Reply with Quote
DATEDIFF is showing the date difference on the Minutes value of the two date/times. It isn't interested in the number of elapsed minutes BETWEEN the two dates (dunno why, but that's how it is!)

Consider these two datetimes. Both are 200 ms apart, but the first one spans a minute boundary, the second doesn't.

select DATEDIFF(Minute, '2006-11-10 00:00:59.900', '2006-11-10 00:01:00.100')
select DATEDIFF(Second, '2006-11-10 00:00:59.900', '2006-11-10 00:01:00.100')
select DATEDIFF(Millisecond, '2006-11-10 00:00:59.900', '2006-11-10 00:01:00.100')

select DATEDIFF(Minute, '2006-11-10 00:00:00.100', '2006-11-10 00:00:00.300')
select DATEDIFF(Second, '2006-11-10 00:00:00.100', '2006-11-10 00:00:00.300')
select DATEDIFF(Millisecond, '2006-11-10 00:00:00.100', '2006-11-10 00:00:00.300')

As Harsh said, you need to evaluate in seconds and divide by 60 to get the "elapsed minutes" - and if you are rounding the result you may actually need to calculate it in milliseconds!

Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 11/10/2006 :  08:34:15  Show Profile  Reply with Quote
Another method

	Elapsed_Minutes = datediff(mi,0,ET-ST),
	Elapsed_Time = ET-ST,
	Start_Time = ST,
	End_time = ET
		ST=convert(datetime,'2006-11-10 05:47:53.497'),
		ET=convert(datetime,'2006-11-10 05:48:10.420')
	) a


Elapsed_Minutes Elapsed_Time            Start_Time              End_time
--------------- ----------------------- ----------------------- -----------------------
0               1900-01-01 00:00:16.923 2006-11-10 05:47:53.497 2006-11-10 05:48:10.420

(1 row(s) affected)

Go to Top of Page


United Kingdom
22859 Posts

Posted - 11/10/2006 :  15:24:09  Show Profile  Reply with Quote

How cool is that!
Go to Top of Page

Patron Saint of Lost Yaks

30421 Posts

Posted - 11/10/2006 :  15:33:44  Show Profile  Visit SwePeso's Homepage  Reply with Quote
I thought it was ET phone home?

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Starting Member

12 Posts

Posted - 11/11/2006 :  05:58:54  Show Profile  Reply with Quote
Thank you people for the help! :)
Go to Top of Page

Starting Member

1 Posts

Posted - 05/29/2013 :  05:08:03  Show Profile  Reply with Quote
select Convert(time(0),(Convert(datetime,'05:48:10.420') - Convert(datetime,'05:47:53.497')),8)
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000