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

后端 未结 8 1704
我寻月下人不归
我寻月下人不归 2021-01-30 08:05

The following query does not update the datetime field:

update table
SET EndDate = \'2009-05-25\'
WHERE Id = 1

I also tried it with no dashes,

相关标签:
8条回答
  • 2021-01-30 08:29

    Normally, it should work.

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

    UPDATE table
    SET EndDate = '2009-05-25 00:00:00.000'
    WHERE Id = 1
    
    0 讨论(0)
  • 2021-01-30 08:34

    If you aren't interested in specifying a time, you can also use the format 'DD/MM/YYYY', however I would stick to a Conversion method, and its relevant ISO format, as you really should avoid using default values.

    Here's an example:

    SET startDate = CONVERT(datetime,'2015-03-11T23:59:59.000',126) WHERE custID = 'F24'

    0 讨论(0)
  • 2021-01-30 08:35
    UPDATE TABLE
       SET EndDate = CAST('2017-12-31' AS DATE)
     WHERE Id = '123'
    
    0 讨论(0)
  • 2021-01-30 08:39

    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
    
    0 讨论(0)
  • 2021-01-30 08:41

    Is there maybe a trigger on the table setting it back?

    0 讨论(0)
  • 2021-01-30 08:47

    That should work, I'd put brackets around [Date] as it's a reserved keyword.

    0 讨论(0)
提交回复
热议问题