Add DATE and TIME fields to get DATETIME field in MySQL

后端 未结 4 946
無奈伤痛
無奈伤痛 2021-01-11 23:16

I am trying to get a DATETIME field from a DATE and a TIME field. none of the functions in MYSQL seems useful.

Is somebody aware how to do this or that if this can e

相关标签:
4条回答
  • 2021-01-11 23:30

    It should be as easy as

    UPDATE table SET datetime_field = CONCAT(date_field, " ", time_field);
    
    0 讨论(0)
  • 2021-01-11 23:35

    @Pekka is right.

    Also you can use CONCAT_WS(seperator, val1, val2,....)

    CONCAT_WS(' ', date_field,time_field)
    
    0 讨论(0)
  • 2021-01-11 23:44
    addtime(date_field, time_field)
    
    0 讨论(0)
  • 2021-01-11 23:53

    Both of the other answers do not convert the date properly if use use a TIME of "838:00:00" which is a valid time according to the mysql manual

    so instead you can try converting the time field to seconds and then adding them
    for example:

    date_field + INTERVAL TIME_TO_SEC(time_field) SECOND
    

    This will convert the date accordingly

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