Create DATETIME from DATE and TIME in MySQL?



You can create DATETIME from DATE and TIME with the help of ADDTIME() function in MySQL. The syntax is as follows −

SELECT ADDTIME(CONVERT(yourDateColumnName,datetime),yourTimeColumnName) as anyVariableName from yourTableName;

To understand the above concept, let us create a table. The query to create a table is as follows −

mysql> create table DateTime −> ( −> DueDate date, −> DueTime time −> ); Query OK, 0 rows affected (1.19 sec)

Now you can insert date and time separately. The query to insert is as follows −

mysql> insert into DateTime values(curdate(),now()); Query OK, 1 row affected (0.08 sec)

Display all records from the table with the help of select statement −

mysql> select *from DateTime;

The following is the output −

+------------+----------+ | DueDate | DueTime | +------------+----------+ | 2018-12-11 | 09:59:07 | +------------+----------+ 1 row in set (0.00 sec)

Now you can implement the syntax we discussed in the beginning to create a datetime from date and time. The query is as follows −

mysql> select addtime(convert(DueDate,datetime),DueTime) as DATETimeDemo from DateTime;

The following is the output displaying datetime created from date and time −

+---------------------+ | DATETimeDemo | +---------------------+ | 2018-12-11 09:59:07 | +---------------------+ 1 row in set (0.00 sec)
Updated on: 2019-07-30T22:30:24+05:30

847 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements