Syntax error What is the easiest way to store date in MySQL database?

What is the easiest way to store date in MySQL database?



To store date in MySQL, use the STR_TO_DATE() method −

insert into yourTableName values(STR_TO_DATE('yourDate', '%d/%m/%Y'));

Let us first create a table −

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

Insert records in the table using insert command −

mysql> insert into DemoTable values(STR_TO_DATE('10/01/2013', '%d/%m/%Y'));
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values(STR_TO_DATE('31/01/2015', '%d/%m/%Y'));
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(STR_TO_DATE('23/04/2019', '%d/%m/%Y'));
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values(STR_TO_DATE('01/03/2019', '%d/%m/%Y'));
Query OK, 1 row affected (0.48 sec)

Display records from the table using select command −

mysql> select *from DemoTable;

This will produce the following output −

+---------------+
| AdmissionDate |
+---------------+
| 2013-01-10    |
| 2015-01-31    |
| 2019-04-23    |
| 2019-03-01    |
+---------------+
4 rows in set (0.00 sec)
Updated on: 2019-07-30T22:30:25+05:30

268 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements