Fetch a single ordered date from a column with MySQL LIMIT


To fetch a single date from a column, use “LIMIT 1. To order it, use ORDER BY clause. Let us first create a table −

mysql> create table DemoTable
-> (
-> DueDate varchar(100)
-> );
Query OK, 0 rows affected (1.16 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('10-06-2019');
Query OK, 1 row affected (0.42 sec)

mysql> insert into DemoTable values('01-12-2016');
Query OK, 1 row affected (0.52 sec)

mysql> insert into DemoTable values('31-01-2018');
Query OK, 1 row affected (0.58 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+------------+
| DueDate    |
+------------+
| 10-06-2019 |
| 01-12-2016 |
| 31-01-2018 |
+------------+
3 rows in set (0.00 sec)

Following is the query to fetch a single ordered date from a column with MySQL LIMIT −

mysql> select *from DemoTable ORDER BY STR_TO_DATE(DueDate, '%d-%m-%Y') LIMIT 1;

Output

This will produce the following output −

+------------+
| DueDate    |
+------------+
| 01-12-2016 |
+------------+
1 row in set (0.00 sec)

Sharon Christine
Sharon Christine

An investment in knowledge pays the best interest

Updated on: 30-Jun-2020

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements