Calculate DateDiff in SQL in Days:Hours:Mins:Secon

2019-02-22 16:39发布

I am currently working an SQL script to calculate the difference between two dates which would give me the result in DD:HH:MI:SEC format. Example: Date 1: 7/30/12 4:00 PM Date 2: 5/4/12 10:31 AM

And the result should be 87:05:29:00

Can you kindly help with the script for this? Regards, Arjun

标签: sql datediff
3条回答
Bombasti
2楼-- · 2019-02-22 17:07

Well, you if want to perform some calculation, you could do this as well:

DECLARE @SecsInADay INT = 60 * 60 * 24
DECLARE @DATE1 DATETIME = CONVERT(DATETIME,'30/07/2012 16:00:00')
DECLARE @DATE2 DATETIME = CONVERT(DATETIME,'04/05/2012 10:31:00')
DECLARE @Days INT = DATEDIFF(DAY, @DATE2, @DATE1)
DECLARE @DiffInSeconds INT = DATEDIFF(SECOND, @DATE2, @DATE1)
DECLARE @TotalDaysInSeconds INT = @Days * @SecsInADay
DECLARE @RemainingHours INT = @DiffInSeconds - @TotalDaysInSeconds
DECLARE @Hours INT = @RemainingHours / 3600
DECLARE @Seconds INT = @RemainingHours % 3600
DECLARE @Minutes INT = @Seconds / 60
DECLARE @RemainingSeconds INT = @Seconds % 60

SELECT
CASE WHEN @Days < 10 THEN '0' + CAST(@Days AS VARCHAR) ELSE CAST(@Days AS VARCHAR) END + ':' +
CASE WHEN @Hours < 10 THEN '0' + CAST(@Hours AS VARCHAR) ELSE CAST(@Hours AS VARCHAR) END + ':' +
CASE WHEN @Minutes < 10 THEN '0' + CAST(@Minutes AS VARCHAR) ELSE CAST(@Minutes AS VARCHAR) END + ':' +
CASE WHEN @RemainingSeconds < 10 THEN '0' + CAST(@RemainingSeconds AS VARCHAR) ELSE CAST(@RemainingSeconds AS VARCHAR) END
查看更多
姐就是有狂的资本
3楼-- · 2019-02-22 17:07

Hi i had a similar problem, took some time to think and here's my solution, I've had the Table with user subscriptions, there sad starting time and closing time, my problem was a bit more complex bit basically it came down to this:

SELECT subscription_id, time_open, time_closed, TIMESTAMPDIFF(DAY,time_open,time_closed) AS Day,

HOUR(sec_to_time(TIMESTAMPDIFF(SECOND,ADDDATE(`time_open`, INTERVAL TIMESTAMPDIFF(DAY,time_open,time_closed) DAY),`time_closed`))) AS Hour,

MINUTE(sec_to_time(TIMESTAMPDIFF(SECOND,ADDDATE(`time_open`, INTERVAL TIMESTAMPDIFF(DAY,time_open,time_closed) DAY),`time_closed`))) AS Minute,

SECOND(sec_to_time(TIMESTAMPDIFF(SECOND,ADDDATE(`time_open`, INTERVAL TIMESTAMPDIFF(DAY,time_open,time_closed) DAY),`time_closed`))) AS Second

FROM `user_subscription`

So basically what this query does is that it calculates days by deducing dates in the first line. Then it adds the number of the full days to the starting time and reduces the closing time my the sum, so what remains is the time in the current day. After that you just select Hours, minutes, and seconds from that. If you want it all together you can concatenate the strings but this way you can use it further, unlike the strings.

查看更多
小情绪 Triste *
4楼-- · 2019-02-22 17:23

If you are using sql-server then you can do this:

declare @x int, 
        @dt1 smalldatetime = '1996-03-25 03:24:16', 
        @dt2 smalldatetime = getdate()

set @x = datediff (s, @dt1, @dt2)


SELECT convert(varchar, @x / (60 * 60 * 24)) + ':'
+ convert(varchar, dateadd(s, @x, convert(datetime2, '0001-01-01')), 108)

Reference here

查看更多
登录 后发表回答