我有这段代码:
cast( DateDiff( MINUTE, S.PlannedStartDateTime, S.PlannedEndDateTime) as decimal (12,6)) / 60 else null end) BasicHours
将日期时间带回小数,即31.416666
,但这确实需要31hr 25mins
我的代码如何显示正确的日期格式?
答案 0 :(得分:1)
又一种选择。
示例强>
Declare @YourTable table (id int,StartTime datetime,EndTime datetime)
Insert Into @YourTable values
(1,'2017-01-01 20:30:15','2017-01-05 22:58:35'),
(2,'2017-01-01 09:00:00','2017-01-01 17:00:00'),
(3,'2017-01-01 09:00:00','2017-01-01 09:05:00')
Select A.ID
,Duration = concat(Seconds/3600,'hr ',Seconds%3600/60,'mins')
From @YourTable A
Cross Apply (values (DateDiff(SECOND,StartTime,EndTime))) B (Seconds)
<强>返回强>
ID Duration
1 98hr 28mins
2 8hr 0mins
3 0hr 5mins
编辑,如果您不想要交叉申请
Select A.ID
,Duration = concat(DateDiff(SECOND,StartTime,EndTime)/3600,'hr ',DateDiff(SECOND,StartTime,EndTime)%3600/60,'mins')
From @YourTable A
应该注意:,有一个限制...... INT的最大值(或2,147,483,647)。所以只要你的跨度不超过:
Years Months Days Hours Minutes Seconds
68 0 19 3 14 7
答案 1 :(得分:0)
这是我的Itzik Ben Gan's DATEDIFFPARTS function
版本CREATE FUNCTION dbo.tfn_DateDiffParts
/* =======================================================================================================
04/06/2017 JL, Created.
Code based off of a similar function created by Itzik Ben-Gan.
(http://sqlmag.com/sql-server/how-compute-date-and-time-difference-parts)
======================================================================================================= */
(
@dt1 AS DATETIME2(7),
@dt2 AS DATETIME2(7)
)
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
SELECT
DateDiffParts = CONCAT(
CAST(V.yy AS VARCHAR(10)) + L.yy,
CAST(V.mm AS VARCHAR(10)) + L.mm,
CAST(V.dd AS VARCHAR(10)) + L.dd,
CAST(V.hh AS VARCHAR(10)) + L.hh,
CAST(V.mi AS VARCHAR(10)) + L.mi,
CAST(V.ss AS VARCHAR(10)) + L.ss
)
FROM
( VALUES (
CASE WHEN @dt1 > @dt2 THEN @dt2 ELSE @dt1 END,
CASE WHEN @dt1 > @dt2 THEN @dt1 ELSE @dt2 END
)
) D (dt1, dt2)
CROSS APPLY ( VALUES (
CAST(D.dt1 AS TIME),
CAST(D.dt2 AS TIME),
DATEDIFF(yy, D.dt1, D.dt2),
DATEDIFF(mm, D.dt1, D.dt2),
DATEDIFF(dd, D.dt1, D.dt2)
)
) A1 (t1, t2, yydiff, mmdiff, dddiff)
CROSS APPLY ( VALUES (
CASE WHEN DATEADD(yy, A1.yydiff, D.dt1) > D.dt2 THEN 1 ELSE 0 END,
CASE WHEN DATEADD(mm, A1.mmdiff, D.dt1) > D.dt2 THEN 1 ELSE 0 END,
CASE WHEN DATEADD(dd, A1.dddiff, D.dt1) > D.dt2 THEN 1 ELSE 0 END
)
) A2 (subyy, submm, subdd)
CROSS APPLY ( VALUES (
CAST(86400000000000 AS BIGINT) * A2.subdd
+ (CAST(1000000000 AS BIGINT) * DATEDIFF(ss, '00:00', A1.t2) + DATEPART(ns, A1.t2))
- (CAST(1000000000 AS BIGINT) * DATEDIFF(ss, '00:00', A1.t1) + DATEPART(ns, A1.t1))
)
) A3 (nsdiff)
CROSS APPLY ( VALUES (
A1.yydiff - A2.subyy,
(A1.mmdiff - A2.submm) % 12,
DATEDIFF(DAY, DATEADD(mm, A1.mmdiff - A2.submm,D.dt1), D.dt2) - A2.subdd,
A3.nsdiff / CAST(3600000000000 AS BIGINT),
A3.nsdiff / CAST(60000000000 AS BIGINT) % 60,
CAST(A3.nsdiff / 1000000000.0 % 60.0 AS DECIMAL(9,1))
)
) V (yy, mm, dd, hh, mi, ss)
CROSS APPLY ( VALUES (
CASE
WHEN V.yy > 1 THEN ' years, '
WHEN V.yy > 0 THEN ' year, '
END,
CASE
WHEN V.mm > 1 THEN ' months, '
WHEN V.mm > 0 THEN ' month, '
WHEN V.yy > 0 THEN ' months, '
END,
CASE
WHEN V.dd > 1 THEN ' days, '
WHEN V.dd > 0 THEN ' day, '
WHEN V.yy > 0 OR V.mm > 0 THEN ' days, '
END,
CASE
WHEN V.hh > 1 THEN ' hours, '
WHEN V.hh > 0 THEN ' hour, '
WHEN V.yy > 0 OR V.mm > 0 OR V.dd > 0 THEN ' hours, '
END,
CASE
WHEN V.mi > 1 THEN ' mins, '
WHEN V.mi > 0 THEN ' min, '
WHEN V.yy > 0 OR V.mm > 0 OR V.dd > 0 OR V.hh > 0 THEN ' mins, '
END,
CASE
WHEN V.ss > 1 THEN ' secs'
WHEN V.ss > 0 THEN ' sec'
WHEN V.yy > 0 OR V.mm > 0 OR V.dd > 0 OR V.hh > 0 OR V.mi > 0 THEN ' secs'
END
)
) L (yy, mm, dd, hh, mi, ss);
GO
示例查询...
SELECT
ddp.DateDiffParts
FROM
dbo.tfn_DateDiffParts('2017-06-01 08:22:11.12345', '2017-06-02 11:30:25.32145') ddp;
功能输出......
DateDiffParts
--------------------------------------------------------------------------------------------------------
1 day, 3 hours, 8 mins, 14.2 secs
答案 2 :(得分:0)
您可以使用以下查询来获得结果 -
select CAST (CONCAT( datediff(HH,'2017-09-05 21:55:00','2017-09-07 08:16:00') , '.' , 60- datediff(MINUTE,'2017-09-05 21:55:00','2017-09-07 08:16:00')%60) as decimal (12,6))
--OUTPUT
35.390000