How to MySQL SELECT by month?


To select by month, use MONTH() function. Let us first create a table −

mysql> create table DemoTable1599
   -> (
   -> Shippingdate datetime
   -> );
Query OK, 0 rows affected (0.78 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1599 values('2019-10-21');
Query OK, 1 row affected (0.28 sec)
mysql> insert into DemoTable1599 values('2018-12-12');
Query OK, 1 row affected (0.35 sec)
mysql> insert into DemoTable1599 values('2015-11-21');
Query OK, 1 row affected (0.34 sec)
mysql> insert into DemoTable1599 values('2017-12-31');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable1599 values('2018-12-26');
Query OK, 1 row affected (0.17 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1599;

This will produce the following output −

+---------------------+
| Shippingdate        |
+---------------------+
| 2019-10-21 00:00:00 |
| 2018-12-12 00:00:00 |
| 2015-11-21 00:00:00 |
| 2017-12-31 00:00:00 |
| 2018-12-26 00:00:00 |
+---------------------+
5 rows in set (0.00 sec)

Following is the query to SELECT by month −

mysql> select * from DemoTable1599
   -> where month(Shippingdate)=12;

This will produce the following output −

+---------------------+
| Shippingdate        |
+---------------------+
| 2018-12-12 00:00:00 |
| 2017-12-31 00:00:00 |
| 2018-12-26 00:00:00 |
+---------------------+
3 rows in set (0.00 sec)

Updated on: 16-Dec-2019

323 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements