How to extract date from string in MySQL?


To extract date from the string in MySQL, use SUBSTRING_INDEX(). Let us first create a table −

mysql> create table DemoTable
   -> (
   -> Title text
   -> );
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John has got joining date.12/31/2018');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('Carol has got joining date.01/11/2019');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values('Sam will arrive at.12/03/2050');
Query OK, 1 row affected (0.87 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------------------------------+
| Title                                 |
+---------------------------------------+
| John has got joining date.12/31/2018  |
| Carol has got joining date.01/11/2019 |
| Sam will arrive at.12/03/2050         |
+---------------------------------------+
3 rows in set (0.00 sec)

Here is the query to extract date from the string in MySQL −

mysql> select substring_index(substring_index(Title,".", -1), ".", 1) from DemoTable;

This will produce the following output −

+----------------------------------------------------------+
| substring_index(substring_index(Title,".", -1), ".", 1)  |
+----------------------------------------------------------+
| 12/31/2018                                               |
| 01/11/2019                                               |
| 12/03/2050                                               |
+----------------------------------------------------------+
3 rows in set (0.00 sec)

Updated on: 13-Dec-2019

465 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements