Find lowest Date (custom) in MySQL?


To find lowest Date(custom) in MySQL, let us first create a table. The query to create a table is as follows:

mysql> create table FindMinimumDate
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> yourDay varchar(2),
   -> yourMonth varchar(2),
   -> yourYear varchar(4),
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.57 sec)

Insert some records in the table using insert command. The query is as follows:

mysql> insert into FindMinimumDate(yourDay,yourMonth,yourYear) values('21','11','2019');
Query OK, 1 row affected (0.10 sec)

mysql> insert into FindMinimumDate(yourDay,yourMonth,yourYear) values('20','10','2020');
Query OK, 1 row affected (0.38 sec)

mysql> insert into FindMinimumDate(yourDay,yourMonth,yourYear) values('03','08','2014');
Query OK, 1 row affected (0.17 sec)

mysql> insert into FindMinimumDate(yourDay,yourMonth,yourYear) values('04','09','2017');
Query OK, 1 row affected (0.12 sec)

mysql> insert into FindMinimumDate(yourDay,yourMonth,yourYear) values('05','07','2013');
Query OK, 1 row affected (0.12 sec)

mysql> insert into FindMinimumDate(yourDay,yourMonth,yourYear) values('25','12','2016');
Query OK, 1 row affected (0.08 sec)

Display all records from the table using select statement. The query is as follows:

mysql> select *from FindMinimumDate;

The following is the output:

+----+---------+-----------+----------+
| Id | yourDay | yourMonth | yourYear |
+----+---------+-----------+----------+
|  1 | 21      | 11        | 2019     |
|  2 | 20      | 10        | 2020     |
|  3 | 03      | 08        | 2014     |
|  4 | 04      | 09        | 2017     |
|  5 | 05      | 07        | 2013     |
|  6 | 25      | 12        | 2016     |
+----+---------+-----------+----------+
6 rows in set (0.00 sec)

Let us now find the lowest Date(Custom) in MySQL. The query is as follows:

mysql> select min(concat(yourYear,'-',yourMonth,'-',yourDay)) as MinimumDate from FindMinimumDate;

The following is the output displaying the lowest date:

+-------------+
| MinimumDate |
+-------------+
| 2013-07-05  |
+-------------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

212 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements