TSQL How can I update only the day part of a DATETIME column from a specific day

StackOverflow https://stackoverflow.com/questions/22155190

  •  19-10-2022
  •  | 
  •  

سؤال

I would like to run a query on a number of incorrectly time-stamped rows (SQL 2005) and replace the DAY value only. My initial thought was just to use the REPLACE function as follows:

-- date correction
UPDATE mytable
SET [date] = REPLACE([date], '2014-02-20', '2014-02-27')
WHERE [date] LIKE '2014-02-20%'

..but that proved to be unsuccessful most likely because of the given column data-type. Any suggestions ?

هل كانت مفيدة؟

المحلول

You could also just make a DATEADD :

UPDATE mytable
SET [date] = DATEADD(DAY, 7, [date])
WHERE [date] >= '2014-02-20' AND [date] < '2014-02-21'
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top