MySQL query to fetch records before currentdate + 2 weeks?

MySQLMySQLi Database

For this, use the below syntax −

select * from yourTableName where yourColumnName < DATE_ADD(CURDATE(), INTERVAL 2 WEEK);

Note: The current date is as follows −

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

Let us first create a table −

mysql> create table DemoTable1607
   -> (
   -> ShippingDate date
   -> )
   -> ;
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1607 values('2019-10-20');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1607 values('2019-11-04');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1607 values('2019-10-05');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable1607 values('2019-09-21');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1607;

This will produce the following output −

+--------------+
| ShippingDate |
+--------------+
| 2019-10-20   |
| 2019-11-04   |
| 2019-10-05   |
| 2019-09-21   |
+--------------+
4 rows in set (0.00 sec)

Here is the query to fetch records where date is before current date + 2 weeks −

mysql> select * from DemoTable1607 where ShippingDate <DATE_ADD(CURDATE(), INTERVAL 2 WEEK);

This will produce the following output −

+--------------+
| ShippingDate |
+--------------+
| 2019-10-20   |
| 2019-10-05   |
| 2019-09-21   |
+--------------+
3 rows in set (0.06 sec)
raja
Published on 16-Dec-2019 07:44:05
Advertisements