我需要使用Sql Server 2008
来计算小时,分钟和秒字段仅包含时间值。
输入
ID HORA_S
-- ----------------------
1 1899-12-30 18:22:50.000
2 1899-12-30 18:22:50.000
3 1899-12-30 18:30:50.000
输出
HOURS MINUTES SECONDS
---- --- --------
55 16 30
在玩了这段代码后,我解决了!!!
SELECT SUM(DATEPART(hh, CAST(HORA_S AS DATETIME)))
+ ( SUM(DATEPART(MINUTE, CAST(HORA_S AS DATETIME))) / 60 )
+ ( SUM(DATEPART(MINUTE, CAST(HORA_S AS DATETIME))) % 60
+ ( SUM(DATEPART(SECOND, CAST(HORA_S AS DATETIME))) / 60 ) ) / 60 AS HORAS,
( SUM(DATEPART(MINUTE, CAST(HORA_S AS DATETIME))) % 60
+ ( SUM(DATEPART(SECOND, CAST(HORA_S AS DATETIME))) / 60 ) ) % 60 AS MINUTOS,
SUM(DATEPART(SECOND, CAST(HORA_S AS DATETIME))) % 60 AS SEGUNDOS FROM Table
答案 0 :(得分:1)
这是一个带有少量代码的工作示例
样本表
DECLARE @T TABLE (ID int, HORA_S datetime)
insert @T values
(1, '1899-12-30 18:22:50.000'),
(2, '1899-12-30 18:22:50.000'),
(3, '1899-12-30 18:30:50.000')
;WITH T(V) AS (
SELECT convert(datetime,SUM(convert(float, 2+hora_s)))
FROM @T
)
SELECT
[HOURS] = floor(convert(float,V)*24),
[MINUTES] = DATEPART(MI,V),
[SECONDS] = DATEPART(SS,V)
FROM T
注意:查询中的2+假定按storing time only
,日期部分始终为1899-12-30
。我知道Delphi将该日期用作0
天。