- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 query to fetch record by year
To fetch record by year, use the YEAR() method in MySQL −
select *from yourTableName where year(yourColumnName)=yourYearValue;
Let us first create a table −
mysql> create table DemoTable -> ( -> CustomerName varchar(100), -> ShippingDate datetime -> ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris','2019-01-21'); Query OK, 1 row affected (0.28 sec) mysql> insert into DemoTable values('Robert','2018-02-21'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('David','2016-04-01'); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+--------------+---------------------+ | CustomerName | ShippingDate | +--------------+---------------------+ | Chris | 2019-01-21 00:00:00 | | Robert | 2018-02-21 00:00:00 | | David | 2016-04-01 00:00:00 | +--------------+---------------------+ 3 rows in set (0.00 sec)
Following is the query to fetch record by year −
mysql> select *from DemoTable -> where year(ShippingDate)=2016;
Output
This will produce the following output −
+--------------+---------------------+ | CustomerName | ShippingDate | +--------------+---------------------+ | David | 2016-04-01 00:00:00 | +--------------+---------------------+ 1 row in set (0.04 sec)
- Related Articles
- MySQL query to fetch date with year and month?
- Fetch a specific record using a single MySQL query with AND & OR operator
- MySQL query to search record with apostrophe?
- Fetch date record that equals today in MySQL
- MySQL query to fetch multiple least values?
- MySQL query to get result by month and year based on condition?
- MySQL query to fetch the maximum cumulative value
- MySQL to fetch records based on a specific month and year?
- How to return the nth record from MySQL query?
- MySQL select query to fetch data with null value?
- MySQL query to find a match and fetch records
- MySQL query to fetch records before currentdate + 2 weeks?
- MySQL query to delete a record with the lowest ID?
- MySQL query to select a record with two exact values?
- MySQL query to place a specific record on the top

Advertisements