SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

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

ashishmgupta
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?

harsh_athalye
Flowing Fount of Yak Knowledge

India
5531 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
India.
"Nothing is Impossible"

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

Kristen
Test

United Kingdom
22403 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!

Kristen
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

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

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

Results:

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)



CODO ERGO SUM
Go to Top of Page

Kristen
Test

United Kingdom
22403 Posts

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

How cool is that!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30242 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

ashishmgupta
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

SuSaya
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  
 New 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.08 seconds. Powered By: Snitz Forums 2000