Search code examples
sql-serverdateadddatepartstring-to-datetime

SQL Server: convert to today then add 8 hours


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


Solution

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