How to convert US date format to MySQL format in INSERT query?


You can use STR_TO_DATE() to convert US date format to MySQL format in INSERT. Let us first create a table −

mysql>create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ShippingDatetime varchar(200)
);
Query OK, 0 rows affected (1.04 sec)

Insert some records in the table using insert command. Here, we are using INSERT to convert US date format −

mysql>insert into DemoTable(ShippingDatetime) values(STR_TO_DATE('01-31-2012 01:23','%m-%d-%Y %H:%i'));
Query OK, 1 row affected (0.14 sec)
mysql>insert into DemoTable(ShippingDatetime) values(STR_TO_DATE('12-01-2018 04:56','%m-%d-%Y %H:%i'));
Query OK, 1 row affected (0.19 sec)
mysql>insert into DemoTable(ShippingDatetime) values(STR_TO_DATE('04-17-2019 10:10','%m-%d-%Y %H:%i'));
Query OK, 1 row affected (0.17 sec)

Following is the query to display all records from the table using select statement −

mysql>select *from DemoTable;

This will produce the following output −

+----+---------------------+
| Id | ShippingDatetime    |
+----+---------------------+
| 1  | 2012-01-31 01:23:00 |
| 2  | 2018-12-01 04:56:00 |
| 3  | 2019-04-17 10:10:00 |
+----+---------------------+
3 rows in set (0.00 sec)

Updated on: 30-Jul-2019

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements