我有一个显示整个月的表 - 这样的票证打开开始日期(2015-07-28 06:40:52:337
)和票据结束结束日期(2015-07-29 07:45:52:337
)。
使用T-SQL DATEDIFF()
我想找出DAYS
,HOURS
,MINUTES
和SECS
之间的区别,并将其放入新的计算列名为TimeDiff
。
我该如何做到这一点?
答案 0 :(得分:0)
使用DATEDIFF
SELECT CAST(DATEDIFF(DAY, [Opened Start Date], [Close End Date]) AS VARCHAR(5)) + ' Day(s), ' +
CAST(DATEDIFF(HOUR, [Opened Start Date], [Close End Date]) AS VARCHAR(5)) + ' Hour(s), ' +
CAST(DATEDIFF(MINUTE, [Opened Start Date], [Close End Date]) AS VARCHAR(5)) + ' Minute(s), ' +
CAST(DATEDIFF(SECOND, [Opened Start Date], [Close End Date]) AS VARCHAR(5)) + ' Second(s), ' AS TimeDiff
FROM yourTable
答案 1 :(得分:0)
你可能需要这样的东西:
CREATE TABLE Table1
(
[starttime] datetime, [endtime] datetime,
TimeDiff as
convert(varchar(3), (DATEDIFF(SECOND, [starttime], [endtime])/86400)) + ' Days, ' +
convert(varchar(2), ((DATEDIFF(SECOND, [starttime], [endtime])%86400)/3600)) + ' Hours, ' +
convert(varchar(2), (((DATEDIFF(SECOND, [starttime], [endtime])%86400)%3600)/60)) + ' Minutes, ' +
convert(varchar(2), (((DATEDIFF(SECOND, [starttime], [endtime])%86400)%3600)%60)) + ' Seconds',
);
以秒为单位计算所有差异并计算每个时间部分。
中的示例