在SQL Server中的datetime2列中添加11小时

时间:2019-03-08 01:22:48

标签: sql-server datetime

我有这样的日期和时间列:

enter image description here

我要添加Time4 + 11:00 hours = 00:53:27:967 + 11:00:00 = 11:53:27:967 然后将其附加到Time1列的时间戳记中 2018-10-19 11:53:27:967

下面是我遵循的过程:

-- Step 1 Trim the Time1 to display only the date
SELECT time1, time4, LEFT(CAST(Time1 AS DATETIME2), LEN(time1) - 9) AS Time5 
FROM table1

并获得以下输出:

enter image description here

--Step 2 Add Time4 + 11
SELECT time4 + '11:00:00.0000000' AS Time6 
FROM DU_GPSTime_2

enter image description here

我在上述步骤中失败。

如果固定了步骤2,最后一步看起来很简单。

--Step 3 Combine time5 + time6
SELECT time7 = time5 + time6 
FROM table1

enter image description here

2 个答案:

答案 0 :(得分:0)

遵循示例

SELECT Time5,  DATEADD(HOUR, 11, CONVERT(time, Time6)) as Time6, CONCAT(CONVERT (date, Time5),' ',DATEADD(HOUR, 11, CONVERT(time, Time6))) AS Time10 FROM TB_EXEMPLO

enter image description here

enter image description here

答案 1 :(得分:0)

只需结合使用DATEADD和DATEDIFF

DATEADD( hh, 11, DATEADD( ms, DATEDIFF( ms, 0, Time4), Time1))