SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Difference between dates displayed in days, hours, minutes and seconds


Difference between dates displayed in days, hours, minutes and seconds

Author
Message
robinson.netdevelop
robinson.netdevelop
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 17
Comments posted to this topic are about the item Difference between dates displayed in days, hours, minutes and seconds
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)

Group: General Forum Members
Points: 162367 Visits: 22752
Didn't we gave some faster alternatives in the forums for this function?


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (390K reputation)SSC Guru (390K reputation)SSC Guru (390K reputation)SSC Guru (390K reputation)SSC Guru (390K reputation)SSC Guru (390K reputation)SSC Guru (390K reputation)SSC Guru (390K reputation)

Group: General Forum Members
Points: 390851 Visits: 42816
And why return a VARCHAR(MAX) value? Are you really going to have a value that needs 2GB?

Plus, this query seems to work just fine:

SELECT CAST(DATEDIFF(DAY,0,DATEADD(SECOND,DATEDIFF(SECOND,StartDate, EndDate),0)) AS VARCHAR(30)) + 'd ' + CONVERT(VARCHAR(8),DATEADD(SECOND,DATEDIFF(SECOND,StartDate, EndDate),0),108)
FROM
(VALUES ('2017-10-03 00:00:00', '2017-10-12 00:00:00')
,('2017-10-05 07:30:00', '2017-10-05 07:55:00')
,('2017-09-01 01:46:00', '2017-09-01 10:55:00')
,('2017-11-21 08:21:00', '2017-12-06 18:00:00')
)dt(StartDate,EndDate);


Not sure what forum thread you are talking about, Luis.


Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Luis Cazares
Luis Cazares
SSC Guru
SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)SSC Guru (162K reputation)

Group: General Forum Members
Points: 162367 Visits: 22752
Lynn Pettis - Thursday, December 28, 2017 11:54 AM
Not sure what forum thread you are talking about, Luis.

Here's the thread.
https://www.sqlservercentral.com/Forums/1913258/Difference-between-dates-displayed-in-days-hours-minutes-and-seconds



Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (264K reputation)SSC Guru (264K reputation)SSC Guru (264K reputation)SSC Guru (264K reputation)SSC Guru (264K reputation)SSC Guru (264K reputation)SSC Guru (264K reputation)SSC Guru (264K reputation)

Group: General Forum Members
Points: 264381 Visits: 12281
Luis Cazares - Thursday, December 28, 2017 10:37 AM
Didn't we gave some faster alternatives in the forums for this function?

I don't if there are any published, but this one is an MTVF, so we know it'll be handicapped right out of the gate.

Here's one I wrote some time ago that performs pretty well. If anyone sees any ways to speed it up, I'm very open to them.

ALTER FUNCTION dbo.DateDiffDHMS(@StartDate Datetime,
@EndDate Datetime) RETURNS TABLE WITH SCHEMABINDING
AS

RETURN (
WITH cteParts AS (
SELECT Days = DATEDIFF(second, @StartDate, @EndDate) / 86400,
Hours = (DATEDIFF(second, @StartDate, @EndDate) / 3600) - (DATEDIFF(second, @StartDate, @EndDate) / 86400 * 24),
Minutes = (DATEDIFF(second, @StartDate, @EndDate) / 60) - (DATEDIFF(second, @StartDate, @EndDate) / 3600 * 60),
Seconds = DATEDIFF(second, @StartDate, @EndDate) % 60
)
SELECT Days, Hours, Minutes, Seconds,
DHMS = CONVERT(Varchar(8), Days) + ':' +
RIGHT('00' + CONVERT(Varchar(2), Hours), 2) + ':' +
RIGHT('00' + CONVERT(Varchar(2), Minutes), 2) + ':' +
RIGHT('00' + CONVERT(Varchar(2), Seconds), 2)
FROM cteParts
);


It does a heap of 100K rows in 120 ms and a heap of 1M rows in 1023 ms.

One limitation is that, because the base difference is in seconds, the max difference will be capped at 24,855 days, or just over 68 years. That's where it runs into the maximum value of an integer. It won't be good for everything, but it's good for everything we use it for.RDA SQL Server Documentation and Trainin For greater differences, it could be adapted to use a larger resolution for the base difference. I figure a resolution of 1 second over 68 years was good enough for how we use it.



Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Ed Wagner
Ed Wagner
SSC Guru
SSC Guru (264K reputation)SSC Guru (264K reputation)SSC Guru (264K reputation)SSC Guru (264K reputation)SSC Guru (264K reputation)SSC Guru (264K reputation)SSC Guru (264K reputation)SSC Guru (264K reputation)

Group: General Forum Members
Points: 264381 Visits: 12281
Luis Cazares - Thursday, December 28, 2017 12:06 PM
Lynn Pettis - Thursday, December 28, 2017 11:54 AM
Not sure what forum thread you are talking about, Luis.

Here's the thread.
https://www.sqlservercentral.com/Forums/1913258/Difference-between-dates-displayed-in-days-hours-minutes-and-seconds

I didn't even know about that thread. Your post here (to the script) was the first I saw of the topic. Thanks for posting it.



Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Sergiy
Sergiy
SSC Guru
SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)SSC Guru (98K reputation)

Group: General Forum Members
Points: 98573 Visits: 14248
I've wrote an article describing the method of age calculations expressed on all sorts of formats:
http://www.sqlservercentral.com/articles/Datetime+conversions/153316/

Amongst others, it can return a date difference in days-hours-minutes-seconds format.

And it does not have the 68 years limitation. :-)
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