
- 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
Extract the month and year in the following format: “mm-yyyy†(month year) along with all columns in MySQL?
For month and year in a specific format, use DATE_FORMAT() along with STR_TO_DATE(). Let us first create a table −
mysql> create table DemoTable1931 ( ShippingDate varchar(40) ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1931 values('10-11-2017'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1931 values('31-01-2019'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1931 values('02-02-2018'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1931 values('10-06-2013'); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1931;
This will produce the following output −
+--------------+ | ShippingDate | +--------------+ | 10-11-2017 | | 31-01-2019 | | 02-02-2018 | | 10-06-2013 | +--------------+ 4 rows in set (0.00 sec)
Following is the query to extract the month and year in the format “mm-yyyy”, along with all columns −
mysql> select date_format(str_to_date(ShippingDate,'%d-%m-%Y'),'%m-%Y') from DemoTable1931;
This will produce the following output −
+-----------------------------------------------------------+ | date_format(str_to_date(ShippingDate,'%d-%m-%Y'),'%m-%Y') | +-----------------------------------------------------------+ | 11-2017 | | 01-2019 | | 02-2018 | | 06-2013 | +-----------------------------------------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Extract the Day / Month / Year from a Timestamp in PHP MySQL?
- Format MySQL date and convert to year-month-day
- Group month and year in MySQL?
- MySQL query to convert YYYY-MM-DD to DD Month, YYYY date format
- Year Month ("Y") Format Specifier in C#
- How can we extract the Year and Month from a date in MySQL?
- Format Year in yyyy format in Java
- MySQL query to fetch date with year and month?
- Format Month in MM format in Java
- How to search dob in a table, which is in the yyyy-mm-dd structure and compare only with a specific yyyy format (year) in MySQL?
- Filter the records of current day, month and year in MySQL?
- MySQL extract year from date format?
- MySQL datatype to store month and year only?
- How to select month and year from dates in MySQL?
- MySQL SELECT query to return records with specific month and year

Advertisements