Tutorialspoint

1 Answer
Arjun Thakur

For this, use STR_TO_DATE(). Following is the syntax −

insert into yourTableName values(STR_TO_DATE(yourDateValue,yourFormatSpecifier));

Let us first create a table −

mysql> create table DemoTable
   (
   ShippingDate date
   );
Query OK, 0 rows affected (0.81 sec)

Insert some records in the table using insert command : Here, we are inserting formatted dates using date formats like m, d, y, etc −

mysql> insert into DemoTable values(STR_TO_DATE('06-01-2019', '%m-%d-%Y'));
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values(STR_TO_DATE('01-31-2019', '%m-%d-%Y'));
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values(STR_TO_DATE('02-01-2018', '%m-%d-%Y'));
Query OK, 1 row affected (0.27 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+--------------+
| ShippingDate |
+--------------+
| 2019-06-01   |
| 2019-01-31   |
| 2018-02-01   |
+--------------+
3 rows in set (0.00 sec)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.