To subtract a day in MySQL, use the DATE_SUB() method. Let us first create a table −
mysql> create table DemoTable -> ( -> AdmissionDate timestamp -> ); Query OK, 0 rows affected (1.05 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('2019-01-01'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('2018-12-31'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2017-03-13'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('2019-01-02'); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+---------------------+ | AdmissionDate | +---------------------+ | 2019-01-01 00:00:00 | | 2018-12-31 00:00:00 | | 2017-03-13 00:00:00 | | 2019-01-02 00:00:00 | +---------------------+ 4 rows in set (0.00 sec)
Here is the query to subtract a day−
mysql> select date_sub(AdmissionDate,interval 1 day) from DemoTable;
This will produce the following output −
+----------------------------------------+ | date_sub(AdmissionDate,interval 1 day) | +----------------------------------------+ | 2018-12-31 00:00:00 | | 2018-12-30 00:00:00 | | 2017-03-12 00:00:00 | | 2019-01-01 00:00:00 | +----------------------------------------+ 4 rows in set (0.00 sec)