MySQL - Select dates that are one week ahead from today?


To get dates that are one week ahead from today, use DATEDIFF. Let us first get the current date −

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2019-12-20 |
+------------+
1 row in set (0.00 sec)

We will first create a table −

mysql> create table DemoTable1990
   (
   ShippingDate date
   );
Query OK, 0 rows affected (0.99 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1990 values('2019-12-13');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1990 values('2019-12-21');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1990 values('2019-12-20');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1990 values('2019-12-24');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1990;

This will produce the following output −

+--------------+
| ShippingDate |
+--------------+
| 2019-12-13   |
| 2019-12-21   |
| 2019-12-20   |
| 2019-12-24   |
+--------------+
4 rows in set (0.00 sec)

Here is the query to select dates that are one week ahead from today −

mysql> select * from DemoTable1990 where datediff(curdate(),ShippingDate)%7=0;

This will produce the following output −

+--------------+
| ShippingDate |
+--------------+
| 2019-12-13   |
| 2019-12-20   |
+--------------+
2 rows in set (0.04 sec)

Updated on: 02-Jan-2020

210 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements