0

日期时间1:2010-11-23 15:30:00.000

日期时间2:2010-11-23 18:30:00.000

DateTime2 - DateTime1 应该是03:00:00

但是当我使用下面的查询时

SELECT (EndTime - StartTime) as Hour From [Day]

它返回

1900-01-01 03:00:00.000

我怎样才能得到它03:00

4

2 回答 2

1

正如你所说,DATEDIFF是合适的。格式化可能最好留给客户端,但是下面将按照您的要求工作

DECLARE @Start DATETIME = '20120716 09:00',
        @End DATETIME = '20120717 12:00'

SELECT  CASE WHEN DATEDIFF(HOUR, @Start, @End) < 10 THEN '0' ELSE '' END + CAST(DATEDIFF(HOUR, @Start, @End) AS VARCHAR) + ':' + 
        RIGHT('0' + CAST(DATEDIFF(MINUTE, @Start, @End) - (DATEDIFF(HOUR, @Start, @End) * 60) AS VARCHAR), 2) [Difference]
于 2012-07-17T07:42:11.337 回答
0

Datediff 函数适合这种操作。

SELECT DATEDIFF ( hour , StartTime, EndTime ) as hour From [Day]

看看http://msdn.microsoft.com/en-us/library/ms189794.aspx

于 2012-07-17T07:19:55.893 回答