
- 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
Want to fetch only the month part from a date in MySQL
To fetch only the month part, use DATE_FORMAT().
Let us first create a table −
mysql> create table DemoTable753 (DueDate datetime); Query OK, 0 rows affected (0.51 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable753 values('2019-06-21'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable753 values('2019-01-31'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable753 values('2018-12-01'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable753 values('2016-11-11'); Query OK, 1 row affected (0.23 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable753;
This will produce the following output -
+---------------------+ | DueDate | +---------------------+ | 2019-06-21 00:00:00 | | 2019-01-31 00:00:00 | | 2018-12-01 00:00:00 | | 2016-11-11 00:00:00 | +---------------------+ 4 rows in set (0.00 sec)
Following is the query to fetch only the month part from a date in MySQL −
mysql> select date_format(DueDate,'%M'),date_format(DueDate,'%m') from DemoTable753;
This will produce the following output -
+---------------------------+---------------------------+ | date_format(DueDate,'%M') | date_format(DueDate,'%m') | +---------------------------+---------------------------+ | June | 06 | | January | 01 | | December | 12 | | November | 11 | +---------------------------+---------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to filter dates in MySQL to fetch date record only for a specific month?
- How can we fetch month and day from a given date in MySQL?
- MySQL query to update only month in date?
- MySQL query to fetch date with year and month?
- Convert DATE timestamp to return only the month name in MySQL
- MySQL query to fetch the latest date from a table with date records
- Fetch date records comparing with the current date’s day and month in MySQL
- Compare only day and month with date field in MySQL?
- Get only the date from datetime in MySQL?
- How to fetch only a single result from a table in Java-MySQL?
- How to part DATE and TIME from DATETIME in MySQL?
- How can fetch records from specific month and year in a MySQL table?
- Create date from day, month, year fields in MySQL?
- Fetch a single ordered date from a column with MySQL LIMIT
- How can we extract the Year and Month from a date in MySQL?

Advertisements