
- 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
Ignoring the year in MySQL Query with date range?
To ignore the year with date range, use the DATE_FORMAT() with the between clause. Let us first create a demo table. The query to create a table is as follows −
mysql> create table igonreYearDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> ShippingDate date -> ); Query OK, 0 rows affected (0.75 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into igonreYearDemo(ShippingDate) values('2016-01-31'); Query OK, 1 row affected (0.16 sec) mysql> insert into igonreYearDemo(ShippingDate) values('2018-01-31'); Query OK, 1 row affected (0.13 sec) mysql> insert into igonreYearDemo(ShippingDate) values('2015-06-23'); Query OK, 1 row affected (0.17 sec) mysql> insert into igonreYearDemo(ShippingDate) values('2015-06-23'); Query OK, 1 row affected (0.15 sec) mysql> insert into igonreYearDemo(ShippingDate) values('2014-02-01'); Query OK, 1 row affected (0.20 sec) mysql> insert into igonreYearDemo(ShippingDate) values('2019-01-31'); Query OK, 1 row affected (0.16 sec) mysql> insert into igonreYearDemo(ShippingDate) values('2019-02-14'); Query OK, 1 row affected (0.11 sec) mysql> insert into igonreYearDemo(ShippingDate) values('2019-03-12'); 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 igonreYearDemo;
Here is the output −
+----+--------------+ | Id | ShippingDate | +----+--------------+ | 1 | 2016-01-31 | | 2 | 2018-01-31 | | 3 | 2015-06-23 | | 4 | 2015-06-23 | | 5 | 2014-02-01 | | 6 | 2019-01-31 | | 7 | 2019-02-14 | | 8 | 2019-03-12 | +----+--------------+ 8 rows in set (0.00 sec)
The following is the query to ignore year with date range −
mysql> select *from igonreYearDemo tbl where DATE_FORMAT(tbl.ShippingDate, '%m-%d') between '01-01' and '03-31';
Here is the output −
+----+--------------+ | Id | ShippingDate | +----+--------------+ | 1 | 2016-01-31 | | 2 | 2018-01-31 | | 5 | 2014-02-01 | | 6 | 2019-01-31 | | 7 | 2019-02-14 | | 8 | 2019-03-12 | +----+--------------+ 6 rows in set (0.00 sec)
- Related Articles
- MySQL query to fetch date with year and month?
- MySQL query to count days in date range with start and end date
- Check if the current date falls in a given date range using MySQL query
- Changing year in MySQL date?
- Get timestamp date range with MySQL Select?
- Display all records ignoring the current date record in MySQL
- How to add a year and two days to a date with a single MySQL query?
- MySQL query to insert row with date?
- MySQL query to fetch the latest date from a table with date records
- MySQL extract year from date format?
- MySQL query to get the dates between range of records displaying student’s Date of Birth?
- MongoDB query to get date records in a range
- MongoDB query to get specific month|year (not date)?
- MySQL query to select records with a particular date?
- Increment date/time value by second with MySQL query?

Advertisements