
- 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
Format date in MySQL to return MonthName and Year?
Use DATE_FORMAT() and set the specifiers to display only the MonthName and Year. Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, AdmissionDate date ); Query OK, 0 rows affected (0.69 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable(AdmissionDate) values('2013-04-21'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable(AdmissionDate) values('2014-01-31'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable(AdmissionDate) values('2016-09-01'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(AdmissionDate) values('2018-12-12'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable(AdmissionDate) values('2019-04-01'); Query OK, 1 row affected (0.15 sec)
Display records from the table using select command −
mysql> select *from DemoTable;
This will produce the following output −
+----+---------------+ | Id | AdmissionDate | +----+---------------+ | 1 | 2013-04-21 | | 2 | 2014-01-31 | | 3 | 2016-09-01 | | 4 | 2018-12-12 | | 5 | 2019-04-01 | +----+---------------+ 5 rows in set (0.00 sec)
Here is the query to format date in MySQL to return MonthName and Year −
mysql> select DATE_FORMAT(AdmissionDate,'%M %Y') AS MonthNameAndYear FROM DemoTable;
This will produce the following output −
+------------------+ | MonthNameAndYear | +------------------+ | April 2013 | | January 2014 | | September 2016 | | December 2018 | | April 2019 | +------------------+ 5 rows in set (0.02 sec)
- Related Articles
- Format MySQL date and convert to year-month-day
- MySQL extract year from date format?
- In which format Year(2) or Year(4) MySQL will return the value of year from date ‘0000-00-00’?
- MySQL query to return the entire date and time based on a string and format
- Changing year in MySQL date?
- Convert MySQL Unix-Timestamp format to date format?
- Select date from MySQL and format to text?
- How to convert Python date format to 10-digit date format for mysql?
- MySQL query to fetch date with year and month?
- Format date with DATE_FORMAT() and STR_TO_DATE() in MySQL
- How to convert US date format to MySQL format in INSERT query?
- How to update the date format in MySQL?
- How to convert a date format in MySQL?
- MySQL ORDER BY Date field not in date format?
- Extract Numeric Date Value from Date Format in MySQL?

Advertisements