tags:

views:

694

answers:

5

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
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
A: 
UPDATE `table`
SET time = ADDTIME(time, INTERVAL 13 Hour);
Zed
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
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
+1  A: 
UPDATE myTable
SET myDateTime = ADDTIME(DATE(myDateTime), @myTimeSpan)
WHERE id = @id;

Documented on MySQl date functions MySQL docs

Mark
A: 

This should do the trick:

UPDATE t1 SET DateTimeField = CONCAT(DATE(DateTimeField),' 12:34:56');

jp