
- 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 extract year from date format?
To extract year from date format, you can use in-built function YEAR() from MySQL. The query is as follows −
mysql> SELECT YEAR(curdate()) as OnlyYearFromCurrentDate;
The following is the output −
+-------------------------+ | OnlyYearFromCurrentDate | +-------------------------+ | 2018 | +-------------------------+ 1 row in set (0.00 sec)
Or you can do in the following way −
mysql> SELECT YEAR(date(now())) as OnlyYearFromCurrentDate;
The following is the output −
+-------------------------+ | OnlyYearFromCurrentDate | +-------------------------+ | 2018 | +-------------------------+ 1 row in set (0.00 sec)
With that, you can even achieve this when column is varchar. Let us first create a table −
mysql> create table ExtractYearDemo −> ( −> YourDueTime varchar(200) −> ); Query OK, 0 rows affected (1.15 sec)
Inserting records into the table with the help of insert command. The query is as follows −
mysql> insert into ExtractYearDemo values('27-10-2011'); Query OK, 1 row affected (0.16 sec) mysql> insert into ExtractYearDemo values('28-11-2012'); Query OK, 1 row affected (0.15 sec) mysql> insert into ExtractYearDemo values('29-12-2018'); Query OK, 1 row affected (0.14 sec) mysql> insert into ExtractYearDemo values('01-01-2019'); Query OK, 1 row affected (0.15 sec)
Displaying all records from the table with the help of SELECT statement. The query is as follows −
mysql> select *from ExtractYearDemo;
The following is the output −
+-------------+ | YourDueTime | +-------------+ | 27-10-2011 | | 28-11-2012 | | 29-12-2018 | | 01-01-2019 | +-------------+ 4 rows in set (0.00 sec)
The following is the query to extract year from table column when column is varchar type −
mysql> select YEAR(STR_TO_DATE(yourDueTime, "%d-%m-%Y")) As OnlyYearFromDate from ExtractYearDemo;
The following is the output displaying year −
+------------------+ | OnlyYearFromDate | +------------------+ | 2011 | | 2012 | | 2018 | | 2019 | +------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Extract Numeric Date Value from Date Format in MySQL?
- How can we extract the Year and Month from a date in MySQL?
- Format date in MySQL to return MonthName and Year?
- Format MySQL date and convert to year-month-day
- In which format Year(2) or Year(4) MySQL will return the value of year from date ‘0000-00-00’?
- How to extract date from string in MySQL?
- Extract the month and year in the following format: “mm-yyyy” (month year) along with all columns in MySQL?
- Select date from MySQL and format to text?
- Create date from day, month, year fields in MySQL?
- Extract the Day / Month / Year from a Timestamp in PHP MySQL?
- Changing year in MySQL date?
- Convert MySQL Unix-Timestamp format to date format?
- Get beginning and end date from a specific year in MySQL
- How to convert Python date format to 10-digit date format for mysql?
- Replace date format with MySQL STR_TO_DATE

Advertisements