
- 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 query to update only month in date?
To update only month in date, use MONTH(). Let us first create a table −
mysql> create table DemoTable861(AdmissionDate date); Query OK, 0 rows affected (1.22 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable861 values('2019-01-21'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable861 values('2018-01-01'); Query OK, 1 row affected (0.49 sec) mysql> insert into DemoTable861 values('2016-01-02'); Query OK, 1 row affected (1.27 sec) mysql> insert into DemoTable861 values('2018-01-14'); Query OK, 1 row affected (0.47 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable861;
This will produce the following output −
+---------------+ | AdmissionDate | +---------------+ | 2019-01-21 | | 2018-01-01 | | 2016-01-02 | | 2018-01-14 | +---------------+ 4 rows in set (0.00 sec)
Following is the query to update only month −
mysql> update DemoTable861 set AdmissionDate=date_add(AdmissionDate,Interval -1 month) where month(AdmissionDate)=01; Query OK, 4 rows affected (0.42 sec) Rows matched : 4 Changed : 4 Warnings : 0
Let us check the table records once again −
mysql> select *from DemoTable861;
This will produce the following output −
+---------------+ | AdmissionDate | +---------------+ | 2018-12-21 | | 2017-12-01 | | 2015-12-02 | | 2017-12-14 | +---------------+ 4 rows in set (0.00 sec)
- Related Articles
- MongoDB query to search date records using only Month and Day
- MySQL query to fetch date with year and month?
- How to update only day portion of MySQL Date?
- Convert DATE timestamp to return only the month name in MySQL
- Compare only day and month with date field in MySQL?
- Want to fetch only the month part from a date in MySQL
- MySQL query to update only a single field in place of NULL
- MySQL query to get current datetime and only current date
- MySQL query to convert YYYY-MM-DD to DD Month, YYYY date format
- How to filter dates in MySQL to fetch date record only for a specific month?
- MongoDB query to update only certain fields?
- MySQL query to extract only the day instead of entire date
- MongoDB query to get specific month|year (not date)?
- MongoDB query to search for Month and Day only?
- MySQL query to convert timestamp to month?

Advertisements