Get maximum date from a list of varchar dates in MySQL



Let us first create a table −

mysql> create table DemoTable (    AdmissionDate varchar(100) ); Query OK, 0 rows affected (0.76 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Sunday, 11 August 2019'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('Friday, 18 October 2019'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Thursday, 18 July 2019'); Query OK, 1 row affected (0.23 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable ;

This will produce the following output −

+-------------------------+ | AdmissionDate | +-------------------------+ | Sunday, 11 August 2019 | | Friday, 18 October 2019 | | Thursday, 18 July 2019 | +-------------------------+ 3 rows in set (0.00 sec)

Following is the query to get the maximum date from a list of varchar dates inserted above −

mysql> select max(str_to_date(AdmissionDate,'%W, %d %M %Y')) AS MaxDate from DemoTable;

This will produce the following output −

+------------+ | MaxDate | +------------+ | 2019-10-18 | +------------+ 1 row in set (0.04 sec)
Updated on: 2019-09-25T10:40:54+05:30

274 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements