SQL Server: convert to today then add 8 hours

对着背影说爱祢 提交于 2020-01-05 07:13:14

问题


nextUpdate can be any date time value in the past. I'm trying to update the nextUpdate field to today's date but keeping the time unchanged and then add 8 hours.

I get

error converting string to datetime

T-SQLe:

UPDATE 
    business.dbo.db_schedule  
SET 
    nextUpdate = DATEADD(hh, 8, CONVERT(datetime, CONVERT(VARCHAR(8), GETDATE(), 111) + ' ' + CONVERT(VARCHAR(8), nextUpdate, 108), 111))  
WHERE
    sno = 8

datetime format in my location is 111


回答1:


UPDATE business.dbo.db_schedule
SET nextUpdate= DATEADD(hh, 8,
                DATEADD(d, DATEDIFF(D,nextUpdate,Getdate()),
                          nextUpdate))
where sno=8


来源:https://stackoverflow.com/questions/12674274/sql-server-convert-to-today-then-add-8-hours

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!