Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Time difference


Time difference

Author
Message
DaveDB
DaveDB
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 215
Hi all,

I have a rather basic question, but having difficulty.... I need to calculate the difference between 2 times. Any help would be appreciated.

Example:

09:28:00 - 09:38:00 = 00:10:00
robert.gerald.taylor
robert.gerald.taylor
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1332 Visits: 1399
Take a look at the DATEDIFF() function: http://technet.microsoft.com/en-us/library/ms189794(v=sql.105).aspx

DATEDIFF ( datepart , startdate , enddate )

HTH,
Rob
Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10334 Visits: 9604
DaveDB (9/6/2013)
Hi all,

I have a rather basic question, but having difficulty.... I need to calculate the difference between 2 times. Any help would be appreciated.

Example:

09:28:00 - 09:38:00 = 00:10:00

You can use the DATEDIFF built-in function.

select datediff(minute, '09:28:00', '09:38:00');




Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
DaveDB
DaveDB
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 215
Thanks
DaveDB
DaveDB
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 215
Is there a way to mark a question as answered in SQL Server Central?
Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10334 Visits: 9604
DaveDB (9/6/2013)
Is there a way to mark a question as answered in SQL Server Central?

Not that I've seen. Most volunteers open a question, read it, see that it's been answered and simply move on to the next one. The question eventually falls off the first page or two, where it won't be seen again until someone looks for it.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
DaveDB
DaveDB
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 215
This isn't working. Please tell me what I am missing...

cast(DATEDIFF(hh, segstart, segstop)+':'+DATEDIFF(mm, segstart, segstop)+':'+DATEDIFF(ss, segstart, segstop)as nvarchar(25)) as a
Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10334 Visits: 9604
If you're sure that all you need is HH:MM:SS, then this will work.

with cte as (
select datediff(second, '09:28:00', '13:38:02') diff)
select
right('00' + convert(varchar(2), (diff % 86400) / 3600), 2) + ':'+
right('00' + convert(varchar(2), (diff % 3600) / 60), 2) + ':' +
right('00' + convert(varchar(2), (diff % 60)), 2)
from cte;



If you want to calculate months, days, hours, minutes and seconds, then we'll need a different approach.


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
ScottPletcher
ScottPletcher
Hall of Fame
Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)Hall of Fame (4K reputation)

Group: General Forum Members
Points: 3953 Visits: 6688
For anything less than 24 hours, you can do this:


SELECT
CONVERT(varchar(8), DATEADD(SECOND, DATEDIFF(SECOND, segstart, segstop), 0), 8)
FROM (
SELECT CAST('20130906 09:28:00' AS datetime) AS segstart, CAST('20130906 09:37:22' AS datetime) AS segstop
) AS test_data




Edit: Added test data.

SQL DBA,SQL Server MVP('07, '08, '09)

Prosecutor James Blackburn, in closing argument in the "Fatal Vision" murders trial: "If in the future, you should cry a tear, cry one for them [the murder victims]. If in the future, you should say a prayer, say one for them. And if in the future, you should light a candle, light one for them."
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search