Can we add minutes to VARCHAR datetime records while applying INSERT statement in MySQL?


Yes, we can add minutes while inserting values in a table.Let us first create a table. Here, we have a column with VARCHAR records where in

mysql> create table DemoTable2026
   -> (
   -> ArrivalTime varchar(20)
   -> );
Query OK, 0 rows affected (0.40 sec)

Insert some records in the table using insert command. We are first converting the VARCHAR date and then adding minutes −

mysql> insert into DemoTable2026 values(date_add(str_to_date('2017-12-01 11:34:45','%Y-%m-%d %H:%i:%s'), INTERVAL 10 MINUTE));
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable2026 values(date_add(str_to_date('2015-01-31 10:00:00','%Y-%m-%d %H:%i:%s'), INTERVAL 5 MINUTE));
Query OK, 1 row affected (0.10 sec)

mysql> insert into DemoTable2026 values(date_add(str_to_date('2017-12-01 11:34:45','%Y-%m-%d %H:%i:%s'), INTERVAL 20 MINUTE));
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable2026;

This will produce the following output −

+---------------------+
| ArrivalTime         |
+---------------------+
| 2017-12-01 11:44:45 |
| 2015-01-31 10:05:00 |
| 2017-12-01 11:54:45 |
+---------------------+
3 rows in set (0.00 sec)

Updated on: 06-Apr-2020

126 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements