Concatenate date and time from separate columns into a single column in MySQL

MySQLMySQLi Database

For this, concatenate both the date and time using CONCAT() function. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> ShippingDate date,
   -> ShippingTime time,
   -> ShippingDatetime datetime
   -> );
Query OK, 0 rows affected (0.50 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(ShippingDate,ShippingTime) values('2019-01-10', '10:40:20');
Query OK, 1 row affected (0.13 sec)

mysql> insert into DemoTable(ShippingDate,ShippingTime) values('2019-06-14', '04:00:10');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+--------------+--------------+------------------+
| ShippingDate | ShippingTime | ShippingDatetime |
+--------------+--------------+------------------+
| 2019-01-10   | 10:40:20     | NULL             |
| 2019-06-14   | 04:00:10     | NULL             |
+--------------+--------------+------------------+
2 rows in set (0.00 sec)

Here is the query to concatenate date and time from separate columns into a single column in MySQL −

mysql> update DemoTable set ShippingDatetime=concat(ShippingDate,' ',ShippingTime);
Query OK, 2 rows affected (0.11 sec)
Rows matched: 2 Changed: 2 Warnings: 0

Let us check table records once again −

mysql> select *from DemoTable;

Output

+--------------+--------------+---------------------+
| ShippingDate | ShippingTime | ShippingDatetime    |
+--------------+--------------+---------------------+
| 2019-01-10   | 10:40:20     | 2019-01-10 10:40:20 |
| 2019-06-14   | 04:00:10     | 2019-06-14 04:00:10 |
+--------------+--------------+---------------------+
2 rows in set (0.00 sec)
raja
Published on 10-Jul-2019 11:35:22
Advertisements