我有一张表显示整个月-像这样的票务开始日期(2015-07-28 06:40:52:337)和像这样的票务结束日期(2015-07-29 07:45:52:337)。

我想使用T-SQL DATEDIFF()找出DAYSHOURSMINUTESSECS的区别,并将其放在新的计算出的列TimeDiff中。

我该怎么做?

最佳答案

使用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

关于sql - 使用DATEDIFF()的计算列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31714838/

10-16 08:54