
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL to fetch records based on a specific month and year?
For this, use MONTH() and YEAR(). Let us first create a table −
mysql> create table DemoTable1846 ( PurchaseDate date ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1846 values('2019-01-10'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1846 values('2019-12-24'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1846 values('2018-09-21'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1846 values('2017-10-26'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1846;
This will produce the following output −
+--------------+ | PurchaseDate | +--------------+ | 2019-01-10 | | 2019-12-24 | | 2018-09-21 | | 2017-10-26 | +--------------+ 4 rows in set (0.00 sec)
Here is the query to fetch records based on a specific month and year
mysql> select * from DemoTable1846 where month(PurchaseDate)=12 and year(PurchaseDate)=2019;
This will produce the following output −
+--------------+ | PurchaseDate | +--------------+ | 2019-12-24 | +--------------+ 1 row in set (0.00 sec)
- Related Questions & Answers
- How can fetch records from specific month and year in a MySQL table?
- MySQL SELECT query to return records with specific month and year
- Do a select in MySQL based only on month and year?
- How to sum selected column values based on specific month records in MySQL?
- MySQL query to fetch date with year and month?
- MySQL query to get result by month and year based on condition?
- Selecting data from a MySQL table based on a specific month?
- MySQL query for grouping and summing the values based on specific records
- Filter the records of current day, month and year in MySQL?
- How to compare Year, Month and Day in a MySQL query and display matching records
- Fetch records containing a specific character twice in MySQL
- Fetch ordered records after a specific limit in MySQL
- Display month names and year from a column with date records with MySQL
- Group month and year in MySQL?
- MySQL datatype to store month and year only?
Advertisements