93

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, but that does not work either.

7
  • 1
    is date the actual column name?
    – Gratzy
    Jun 29, 2010 at 19:37
  • 1
    not actually, it is EndDate, but I just shortened it to Date.
    – Xaisoft
    Jun 29, 2010 at 19:39
  • do you get an error or just no results?
    – Gratzy
    Jun 29, 2010 at 19:40
  • What's the data type for the EndDate column?
    – OMG Ponies
    Jun 29, 2010 at 19:41
  • It says 1 row affected, but when I check, the date has not changed.
    – Xaisoft
    Jun 29, 2010 at 19:41

8 Answers 8

177

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
2
  • 1
    great thanks works well in SQL server 2016. I just use variables and all set. Sep 4, 2018 at 0:41
  • I used that and added the time after the date and it worked
    – FernandoZ
    Oct 30, 2018 at 16:15
42

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
3
  • 1
    This worked, but I tried it before without all zeros and it did not work. Why?
    – Xaisoft
    Jun 29, 2010 at 19:44
  • I tried it with '2009-05-25 00:02:01.000', but it didn't work.
    – Xaisoft
    Jun 29, 2010 at 19:44
  • Nice, seriously dont have any idea. Because you wrote without time information, and SQL must attach default 00:00:00.000 automaticly. So it must be worked. Jun 29, 2010 at 19:55
12

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...

3
  • The date format is why I was thinking being explicit about the conversion would be a good idea...
    – OMG Ponies
    Jun 29, 2010 at 19:47
  • Good stuff Remus, SET DATEFORMAT is really effective when writing SQL for environments with different localizations
    – Matt R
    Jun 18, 2013 at 13:46
  • Awesome! This worked for me and saved me a few steps because the field I'm converting was already using this format, i.e., no need to convert it to ISO or other format first.
    – Yogi
    Mar 5, 2018 at 0:52
10

Using a DateTime parameter is the best way. However, if you still want to pass a DateTime as a string, then the CAST should not be necessary provided that a language agnostic format is used.

e.g.

Given a table created like :

create table t1 (id int, EndDate DATETIME)
insert t1 (id, EndDate) values (1, GETDATE())

The following should always work :

update t1 set EndDate = '20100525' where id = 1 -- YYYYMMDD is language agnostic

The following will work :

SET LANGUAGE us_english
update t1 set EndDate = '2010-05-25' where id = 1

However, this won't :

SET LANGUAGE british
update t1 set EndDate = '2010-05-25' where id = 1  

This is because 'YYYY-MM-DD' is not a language agnostic format (from SQL server's point of view) .

The ISO 'YYYY-MM-DDThh:mm:ss' format is also language agnostic, and useful when you need to pass a non-zero time.

More info : http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes

5
UPDATE TABLE
   SET EndDate = CAST('2017-12-31' AS DATE)
 WHERE Id = '123'
3

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'

2

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

0
1

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

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.