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

MySQLMySQLi 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)
raja
Published on 29-Apr-2019 15:42:58
Advertisements