MySQL query to select all entries from a particular month



To select all entries from a particular month in MySQL, use the monthname() or month() function.

The syntax is as follows.

select *from yourTableName where monthname(yourColumnName)='yourMonthName';

To understand the above syntax, let us create a table. The query to create a table is as follows

mysql> create table selectAllEntriesDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.63 sec)

Insert some records in the table using insert command.

The query is as follows

mysql> insert into selectAllEntriesDemo(ShippingDate) values('2019-01-21'); Query OK, 1 row affected (0.24 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2018-02-24'); Query OK, 1 row affected (0.15 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2010-10-22'); Query OK, 1 row affected (0.20 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2011-04-12'); Query OK, 1 row affected (0.12 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2013-02-10'); Query OK, 1 row affected (0.18 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2014-02-15'); Query OK, 1 row affected (0.16 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2016-06-14'); Query OK, 1 row affected (0.18 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2017-02-14'); Query OK, 1 row affected (0.51 sec) mysql> insert into selectAllEntriesDemo(ShippingDate) values('2015-03-29'); Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement.

The query is as follows.

mysql> select *from selectAllEntriesDemo;

The following is the output.

+----+---------------------+ | Id | ShippingDate | +----+---------------------+ | 1 | 2019-01-21 00:00:00 | | 2 | 2018-02-24 00:00:00 | | 3 | 2010-10-22 00:00:00 | | 4 | 2011-04-12 00:00:00 | | 5 | 2013-02-10 00:00:00 | | 6 | 2014-02-15 00:00:00 | | 7 | 2016-06-14 00:00:00 | | 8 | 2017-02-14 00:00:00 | | 9 | 2015-03-29 00:00:00 | +----+---------------------+ 9 rows in set (0.00 sec)

The following is the query to select all entries from a particular month:

mysql> select *from selectAllEntriesDemo where monthname(ShippingDate)='February';

Here is the output.

+----+---------------------+ | Id | ShippingDate | +----+---------------------+ | 2 | 2018-02-24 00:00:00 | | 5 | 2013-02-10 00:00:00 | | 6 | 2014-02-15 00:00:00 | | 8 | 2017-02-14 00:00:00 | +----+---------------------+ 4 rows in set (0.00 sec)

Here is an alternate query.

mysql> select *from selectAllEntriesDemo where month(ShippingDate)=2;

The following is the output.

+----+---------------------+ | Id | ShippingDate | +----+---------------------+ | 2 | 2018-02-24 00:00:00 | | 5 | 2013-02-10 00:00:00 | | 6 | 2014-02-15 00:00:00 | | 8 | 2017-02-14 00:00:00 | +----+---------------------+ 4 rows in set (0.04 sec)
Updated on: 2019-07-30T22:30:25+05:30

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements