Update only Time in a mysql DateTime field

Viewed 55525

How can I update only the time in an already existing DateTime field in MySQL? I want the date to stay the same.

12 Answers

Try this:

UPDATE yourtable 
SET yourcolumn = concat(date(yourcolumn), ' 21:00:00')
WHERE Id = yourid;

Try this:

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

I used ADDTIME in the following way

Earlier in my cloud server, the DateTime was set to UTC but after changing the DateTime to Asia/Kolkata ie UTC 5:30 I wanted the same to reflect in my database tables.

I wanted to update the created_at and updated_at column by 5 hours 30 minutes. I did the following

To update all the rows of the table

UPDATE 
    products 
SET 
    created_at = ADDTIME(created_at, '5:30:0'), 
    updated_at = ADDTIME(updated_at, '5:30:0') 

You can omit the WHERE condition if you want to update all the records, but since my new records were updated with proper values. So only my rows below id less than 2500 must be updated

UPDATE 
    products 
SET 
    created_at = ADDTIME(created_at, '5:30:0'), 
    updated_at = ADDTIME(updated_at, '5:30:0') 
WHERE
    id < 2500;
Related