Creating DATETIME from DATE and TIME

前端 未结 6 1444
后悔当初
后悔当初 2020-12-05 04:23

Is there way in MySQL to create DATETIME from a given attribute of type DATE and a given attribute of type TIME?

相关标签:
6条回答
  • 2020-12-05 04:38
    select timestamp('2003-12-31 12:00:00','12:00:00'); 
    

    works, when the string is formatted correctly. Otherwise, you can just include the time using str_to_date.

    select str_to_date('12/31/2003 14:59','%m/%d/%Y %H:%i');
    
    0 讨论(0)
  • 2020-12-05 04:42

    You could use ADDTIME():

    ADDTIME(CONVERT(date, DATETIME), time)
    
    • date may be a date string or a DATE object.
    • time may be a time string or a TIME object.

    Tested in MySQL 5.5.

    0 讨论(0)
  • 2020-12-05 04:46

    Without creating and parsing strings, just add an interval to the date:

    set @dt_text = '1964-05-13 15:34:05.757' ;
    set @d = date(@dt_text) ;
    set @t = time(@dt_text) ;
    select @d, @t, @d + interval time_to_sec( @t ) second;
    

    However this truncates the microseconds.

    I agree with Muki - be sure to take account of time zones and daylight savings time!

    0 讨论(0)
  • 2020-12-05 04:55

    Copied from the MySQL Documentation:

    TIMESTAMP(expr), TIMESTAMP(expr1,expr2)

    With a single argument, this function returns the date or datetime expression expr as a datetime value. With two arguments, it adds the time expression expr2 to the date or datetime expression expr1 and returns the result as a datetime value.

    mysql> SELECT TIMESTAMP('2003-12-31');
        -> '2003-12-31 00:00:00'
    mysql> SELECT TIMESTAMP('2003-12-31 12:00:00','12:00:00');
        -> '2004-01-01 00:00:00'
    
    0 讨论(0)
  • 2020-12-05 04:57

    To get a true DATETIME value from your two separate DATE and TIME values:

    STR_TO_DATE(CONCAT(date, ' ', time), '%Y-%m-%d %H:%i:%s')
    
    0 讨论(0)
  • 2020-12-05 04:58
    datetime = CONCAT(date, ' ', time);
    
    0 讨论(0)
提交回复
热议问题