Is there a way to subtract number of days from a date in MySQL?

MySQLMySQLi Database

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)
raja
Published on 23-Aug-2019 15:57:37
Advertisements