How to filter dates in MySQL to fetch date record only for a specific month?

MySQLMySQLi Database

Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable values('2018-01-21');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('2019-08-13');
Query OK, 1 row affected (0.56 sec)
mysql> insert into DemoTable values('2019-07-08');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('2016-02-12');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+---------------+
| AdmissionDate |
+---------------+
| 2018-01-21    |
| 2019-08-13    |
| 2019-07-08    |
| 2016-02-12    |
+---------------+
4 rows in set (0.00 sec)

Following is the query to filter dates in MySQL and fetch date record only for August month −

mysql> select *from DemoTable where AdmissionDate like '2019-08%';

This will produce the following output −

+---------------+
| AdmissionDate |
+---------------+
| 2019-08-13    |
+---------------+
1 row in set (0.04 sec)
raja
Published on 25-Sep-2019 11:32:49
Advertisements