
- 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
How to subtract 30 days from the current datetime in MySQL?
To subtract 30 days from current datetime, first we need to get the information about current date time, then use the now() method from MySQL. The now() gives the current date time.
The method to be used for this is DATE_SUB() from MySQL. Here is the syntax to subtract 30 days from current datetime.
The syntax is as follows −
DATE_SUB(NOW(),INTERVAL 30 DAY);
The above syntax calculates the current datetime first and in the next step, subtracts 30 days. Let us first seethe query to get the current datetime −
mysql> select now();
Here is the output −
+---------------------+ | now() | +---------------------+ | 2018-11-23 16:38:43 | +---------------------+ 1 row in set (0.00 sec)
Now, let us used the date_sub() method to subtract 30 days. The query is as follows −
mysql> select date_sub(now(),interval 30 day);
Here is the output displaying a date after subtracting 30 days −
+---------------------------------+ | date_sub(now(),interval 30 day) | +---------------------------------+ | 2018-10-24 16:38:50 | +---------------------------------+ 1 row in set (0.00 sec)
To get only the date value in the result, you can use curdate() instead of now(). Here is the demo of curdate().
The query is as follows −
mysql> select date_sub(curdate(),interval 30 day);
The following is the output −
+-------------------------------------+ | date_sub(curdate(),interval 30 day) | +-------------------------------------+ | 2018-10-24 | +-------------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to subtract 10 days from the current datetime in MySQL?
- How to subtract 3 hours from a datetime in MySQL?
- Subtract days from current date using Calendar.DATE in Java
- C# DateTime to add days to the current date
- MySQL Datetime to add days?
- How to add 30 minutes to datetime in MySQL?
- How to subtract days from a date in JavaScript?
- Fetch datetime row from exactly past 7 days records in MySQL
- How to get the records of the last two days from the current date in MySQL?
- Is there a way to subtract number of days from a date in MySQL?
- Java Program to subtract 40 days from the calendar
- MySQL query to get current datetime and only current date
- Insert current date in datetime format MySQL?
- Add 11 days to current date in MySQL
- MySQL DateTime Now()+5 days/hours/minutes/seconds?
