Add DATE and TIME fields to get DATETIME field in MySQL
Asked Answered
C

4

11

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 even be done? :)

Ciaphus answered 22/7, 2010 at 9:43 Comment(0)
I
14

It should be as easy as

UPDATE table SET datetime_field = CONCAT(date_field, " ", time_field);
Insurgent answered 22/7, 2010 at 9:45 Comment(0)
H
9

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

Henotheism answered 15/6, 2014 at 10:11 Comment(0)
T
4
addtime(date_field, time_field)
Tweak answered 27/4, 2017 at 8:9 Comment(4)
please consider the following How do I write a good answer?Evangel
also , are you sure that addtime is mysql defined function ?Evangel
Hey Hassan - I don't see why it's necessary to add anything more to my answer - people want a quick solution. Here's the mysql function documented: dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.htmlTweak
if date_field is DATE type you'll need to cast it to DATETIME first as it won't do automatic type promotion from DATE to DATETIMEHydrokinetics
T
1

@Pekka is right.

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

CONCAT_WS(' ', date_field,time_field)
Thistledown answered 22/7, 2010 at 9:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.