我想要datetime
1day , 2hours, 3min.
列的差异
我试过这个,
DECLARE @end datetime2 = '2013-11-13 09:35:49.007'
DECLARE @start datetime2 = '2013-11-12 17:34:11.533'
select datediff(day, @start, @end) days,
datediff(HOUR, @start, @end) Hours,
datediff(MINUTE, @start, @end) Minutes
输出
1day 16hours 961minutes.
但我想要输出1day 4hours 30min.
需要一些帮助来解决这个问题。谢谢
答案 0 :(得分:3)
样品的差异是16小时,1分钟和38秒。你在哪里提出1天,4小时30分钟? 17:34是下午5:34,而不是早上5:34。
这是从日期差异中执行描述性文本的代码。注意:要解决0周,天,小时等的边缘情况,您必须在进行演员表之前进行检查。
DECLARE @end datetime2 = '2013-11-13 09:35:49.007'
DECLARE @start datetime2 = '2013-11-12 17:34:11.533'
SELECT ''
+ CAST(DATEDIFF(second, @start, @end) / 60 / 60 / 24 / 7 AS NVARCHAR(50)) + ' weeks, '
+ CAST(DATEDIFF(second, @start, @end) / 60 / 60 / 24 % 7 AS NVARCHAR(50)) + ' days, '
+ CAST(DATEDIFF(second, @start, @end) / 60 / 60 % 24 AS NVARCHAR(50)) + ' hours, '
+ CAST(DATEDIFF(second, @start, @end) / 60 % 60 AS NVARCHAR(50)) + ' minutes and '
+ CAST(DATEDIFF(second, @start, @end) % 60 AS NVARCHAR(50)) + ' seconds.';
<强>输出:强>
0 weeks, 0 days, 16 hours, 1 minutes and 38 seconds.