How to get string as date in MySQL with dates as dot format specifier?

MySQLMySQLi Database

To get string as date, use STR_TO_DATE() method. Let us first create a table −

mysql> create table DemoTable1445
   -> (
   -> AdmissionDate varchar(20)
   -> );
Query OK, 0 rows affected (0.46 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1445 values('01.10.2019');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable1445 values('31.12.2018');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable1445 values('01.02.2017');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select −

mysql> select * from DemoTable1445;

This will produce the following output −

+---------------+
| AdmissionDate |
+---------------+
| 01.10.2019    |
| 31.12.2018    |
| 01.02.2017    |
+---------------+
3 rows in set (0.00 sec)

Following is the query to get string as date in MySQL −

mysql> update DemoTable1445 set AdmissionDate=str_to_date(AdmissionDate,'%.%.%');
Query OK, 3 rows affected (0.15 sec)
Rows matched: 3  Changed: 3 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1445;

This will produce the following output −

+---------------+
| AdmissionDate |
+---------------+
| 2019-10-01    |
| 2018-12-31    |
| 2017-02-01    |
+---------------+
3 rows in set (0.00 sec)
raja
Published on 10-Dec-2019 05:28:35
Advertisements