How can I update only the time in an already existing DateTime field in MySQL? I want the date to stay the same.
A:
UPDATE myTable
SET myDateTime = ADDTIME(myDateTime, @myTimeSpan)
WHERE id = @id;
For exact syntax of function, see this.
colithium
2009-08-25 07:28:11
The `ADDTIME` function adds a time to the the existing. This means that for this to work, you expect `myDateTime` to be have only the date portion set (and time portion is midnight).
awe
2009-08-25 07:48:32
A:
Well, exactly what you are asking for is not possible. The date and time components can't be updated separately, so you have to calculate the new DateTime value from the existing one so that you can replace the whole value.
Guffa
2009-08-25 07:39:36
Well it is possible to do it, not specifically targeting the time portion of the datetime field, but you can do something to get the desired result.
awe
2009-08-25 07:58:45
+1
A:
UPDATE myTable
SET myDateTime = ADDTIME(DATE(myDateTime), @myTimeSpan)
WHERE id = @id;
Documented on MySQl date functions MySQL docs
Mark
2009-09-08 23:41:52
A:
This should do the trick:
UPDATE t1 SET DateTimeField = CONCAT(DATE(DateTimeField),' 12:34:56');
jp
2010-01-26 08:15:26