How do you update a DateTime field in T-SQL? How do you update a DateTime field in T-SQL? sql-server sql-server

How do you update a DateTime field in T-SQL?


When in doubt, be explicit about the data type conversion using CAST/CONVERT:

UPDATE TABLE   SET EndDate = CAST('2009-05-25' AS DATETIME) WHERE Id = 1


Normally, it should work.

But can you try this? I don't have SQL on my home PC, I can't try myself

UPDATE tableSET EndDate = '2009-05-25 00:00:00.000'WHERE Id = 1


The string literal is pased according to the current dateformat setting, see SET DATEFORMAT. One format which will always work is the '20090525' one.

Now, of course, you need to define 'does not work'. No records gets updated? Perhaps the Id=1 doesn't match any record...

If it says 'One record changed' then perhaps you need to show us how you verify...