
- 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
Changing year in MySQL date?
To change the year in MySQL date, you need to use DATE_FORMAT() function with UPDATE command. The syntax is as follows.
UPDATE yourTableName SET yourDateColumnName = DATE_FORMAT(yourDateColumnName ,'yourYearValue-%m-%d');
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table ChangeYear -> ( -> id int not null auto_increment, -> ArrivalTime date, -> PRIMARY KEY(id) -> ); Query OK, 0 rows affected (0.83 sec)
Insert some records in the table using insert command −
mysql> insert into ChangeYear(ArrivalTime) values(date_add(now(),interval -2 year)); Query OK, 1 row affected, 1 warning (0.20 sec) mysql> insert into ChangeYear(ArrivalTime) values('2012-10-24'); Query OK, 1 row affected (0.18 sec) mysql> insert into ChangeYear(ArrivalTime) values('2016-3-21'); Query OK, 1 row affected (0.19 sec) mysql> insert into ChangeYear(ArrivalTime) values('2015-4-24'); Query OK, 1 row affected (0.20 sec) mysql> insert into ChangeYear(ArrivalTime) values(curdate()); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement. The query is as follows.
mysql> select *from ChangeYear;
The following is the output.
+----+-------------+ | id | ArrivalTime | +----+-------------+ | 1 | 2017-01-07 | | 2 | 2012-10-24 | | 3 | 2016-03-21 | | 4 | 2015-04-24 | | 5 | 2019-01-07 | +----+-------------+ 5 rows in set (0.00 sec)
Here is the query to update only the year in the date column. For our example, let us change the year to 2019 −
mysql> update ChangeYear -> set ArrivalTime = DATE_FORMAT(ArrivalTime,'2019-%m-%d'); Query OK, 4 rows affected (0.12 sec) Rows matched − 5 Changed − 4 Warnings − 0
Check all the updated records from the table using select statement. The query is as follows −
mysql> select *from ChangeYear;
The following is the output displaying the updated year 2019, but rest of the date remains the same −
+----+-------------+ | id | ArrivalTime | +----+-------------+ | 1 | 2019-01-07 | | 2 | 2019-10-24 | | 3 | 2019-03-21 | | 4 | 2019-04-24 | | 5 | 2019-01-07 | +----+-------------+ 5 rows in set (0.00 sec)
- Related Articles
- Changing data type from date to date/time in MySQL?
- MySQL - Changing year of dates from 2020 to 2011?
- MySQL extract year from date format?
- Create date from day, month, year fields in MySQL?
- Ignoring the year in MySQL Query with date range?
- Format date in MySQL to return MonthName and Year?
- Update MySQL date and increment by one Year?
- MySQL query to fetch date with year and month?
- Format MySQL date and convert to year-month-day
- Get beginning and end date from a specific year in MySQL
- In which format Year(2) or Year(4) MySQL will return the value of year from date ‘0000-00-00’?
- How can we extract the Year and Month from a date in MySQL?
- What are the different ways in MySQL to add ‘half year interval’ in date?
- Add a single year to all the date records in a MySQL table column
- Display month names and year from a column with date records with MySQL

Advertisements