
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to fetch date with year and month?
For year and month date fetching, you can use YEAR() and MONTH() function in MySQL. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, ShippingDate datetime ); Query OK, 0 rows affected (0.48 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(ShippingDate) values('2019-01-31'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(ShippingDate) values('2018-12-01'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(ShippingDate) values('2019-06-02'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(ShippingDate) values('2019-11-18'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+-----------------------+ | Id | ShippingDate | +----+-----------------------+ | 1 | 2019-01-31 00 :00 :00 | | 2 | 2018-12-01 00 :00 :00 | | 3 | 2019-06-02 00 :00 :00 | | 4 | 2019-11-18 00 :00 :00 | +----+-----------------------+ 4 rows in set (0.00 sec)
Following is the query to fetch date on the basis of a specific year and month −
mysql> select *from DemoTable where ( year(ShippingDate)='2019') and (month(ShippingDate)='06');
Output
+----+-----------------------+ | Id | ShippingDate | +----+-----------------------+ | 3 | 2019-06-02 00 :00 :00 | +----+-----------------------+ 1 row in set (0.07 sec)
- Related Articles
- MySQL SELECT query to return records with specific month and year
- MySQL query to fetch record by year
- Format MySQL date and convert to year-month-day
- MongoDB query to get specific month|year (not date)?
- MySQL to fetch records based on a specific month and year?
- Display month names and year from a column with date records with MySQL
- Fetch date records comparing with the current date’s day and month in MySQL
- MySQL query to fetch the latest date from a table with date records
- Ignoring the year in MySQL Query with date range?
- MySQL query to update only month in date?
- MySQL query to get result by month and year based on condition?
- How can fetch records from specific month and year in a MySQL table?
- Create date from day, month, year fields in MySQL?
- Java Program to get current date, year and month
- MySQL query to fetch date records greater than the current date after adding days with INTERVAL?

Advertisements