Update only Time in a mysql DateTime field

前端 未结 12 1431
北海茫月
北海茫月 2020-12-09 07:44

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

相关标签:
12条回答
  • 2020-12-09 08:15
    UPDATE myTable
    SET myDateTime = ADDTIME(myDateTime, @myTimeSpan)
    WHERE id = @id;
    

    For exact syntax of function, see this.

    0 讨论(0)
  • 2020-12-09 08:21

    Try this:

    UPDATE yourtable 
    SET yourcolumn = concat(date(yourcolumn), ' 21:00:00')
    WHERE Id = yourid;
    
    0 讨论(0)
  • 2020-12-09 08:22
    UPDATE `table`
    SET time = ADDTIME(time, INTERVAL 13 Hour);
    
    0 讨论(0)
  • 2020-12-09 08:23

    I have solved in this way:

    UPDATE table
    SET myDateTime = CONCAT_WS(' ',DATE(myDateTime), CURTIME())
    WHERE id = @id;
    

    Obviously you should change CURTIME() with your desired time.

    0 讨论(0)
  • 2020-12-09 08:26

    Asuming you have a DATE field and TIME field and want to inject the time into the date, try this:

    UPDATE mytable
    SET mydatefield = ADDTIME( DATE_FORMAT(mydatefield,'%Y-%m-%d 00:00:00'), mydatefield)
    WHERE myid = ...
    
    0 讨论(0)
  • 2020-12-09 08:28
    UPDATE myTable
    SET myDateTime = ADDTIME(DATE(myDateTime), @myTimeSpan)
    WHERE id = @id;
    

    Documented on MySQl date functions MySQL docs

    0 讨论(0)
提交回复
热议问题