I am trying to update a datetime field on a table (MyTable1) from a date field on another table (MyTable2).
- DateTime value in datetime field in MyTable1 is stored following below format yyyy-mm-dd HH:mm:ss.fff
- Date value in date field in MyTable2 is stored following below format yyyy-mm-dd
So taking into account this, I perform below 2 attempts without success. What am I doing wrong?
ATTEMPT #1:
UPDATE tblToUpdate
SET tblToUpdate.DateTimeField = fromTbl.DateField
FROM MyTable1 tblToUpdate INNER JOIN MyTable2 fromTbl on tblToUpdate.Id = fromTbl.Id
This produces below error:
The conversion of a date data type to a datetime data type resulted in an out-of-range value
ATTEMPT #2:
UPDATE tblToUpdate
SET tblToUpdate.DateTimeField = (case when fromTbl.DateField is NULL
then NULL
else format(fromTbl.DateField, 'yyyy-mm-dd HH:mm:ss.fff') end)
FROM MyTable1 tblToUpdate INNER JOIN MyTable2 fromTbl on tblToUpdate.Id = fromTbl.Id
This produces below error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
datevalue? Data type DATE has a range of accepted values from 01-01-0001 through 12-31-9999, data type DATETIME has a range of accepted values from 01-01-1753 through 12-31-9999. - HoneyBadgerSELECT MIN(DateField) FROM yourTable. If the result is < 01-01-1753 that's probably the issue. - HoneyBadger