
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is there a way to subtract number of days from a date in MySQL?
Yes, you can use date_sub() to subtract number of days from a date. Following is the syntax −
select date_sub(yourColumnName,Interval yourAmountOfDays day) from yourTableName;
Let us first create a table −
mysql> create table DemoTable660(AdmissionDate datetime); Query OK, 0 rows affected (0.82 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable660 values('2018-01-24'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable660 values('2019-07-10'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable660 values('2020-11-20'); Query OK, 1 row affected (0.59 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable660;
This will produce the following output −
+---------------------+ | AdmissionDate | +---------------------+ | 2018-01-24 00:00:00 | | 2019-07-10 00:00:00 | | 2020-11-20 00:00:00 | +---------------------+ 3 rows in set (0.00 sec)
Following is the query to subtract number of days from a date in MySQL −
mysql> select date_sub(AdmissionDate,Interval 5 day) from DemoTable660;
This will produce the following output −
+----------------------------------------+ | date_sub(AdmissionDate,Interval 5 day) | +----------------------------------------+ | 2018-01-19 00:00:00 | | 2019-07-05 00:00:00 | | 2020-11-15 00:00:00 | +----------------------------------------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- How to subtract days from a date in JavaScript?
- How to subtract number of days from a date to get the previous date in R?
- Is there a way to make a list from a MySQL table in Java?
- Subtract days from current date using Calendar.DATE in Java
- Is there a way to change the date format in php?
- MySQL add days to a date?
- Is there a way to know your current username in MySQL?
- Is there any way to use values from a JSON object in a MySQL Select statement?
- Is there a way to retrieve the minimum value of fields in MySQL?
- Is there a way to limit the number of records in a certain MongoDB collection?
- Is there a way to select a value which matches partially in MySQL?
- In MySQL, is there a way to turn column records into a list?
- Is there an easy way to rename a table in a MySQL procedure?
- MySQL query to delete a DATE older than 30 days from another date?
- Fastest way to search for a date from the date records in MySQL
Advertisements