I have a table filled with a lot of rows and I need to select all the rows that are less than a year old till now.
The table (called orders
) has a DateTime
column named order_date
, that's the field that determines when the order was placed.
How can I select all the records that have an order_date
between now and a full year ago?
Instead of approximating the "current" date by selecting the MAX(date) the code could reference CAST(GETDATE() as DATE) to access the system datetime and cast it as type DATE. where [date] > dateadd(month, -6, cast(getdate() as date));
SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).
Here's the SQL query to get records from last 7 days in MySQL. In the above query we select those records where order_date falls after a past interval of 7 days. We use system function now() to get the latest datetime value, and INTERVAL clause to calculate a date 7 days in the past.
select * from orders where order_date >= DATE_SUB(NOW(),INTERVAL 1 YEAR);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With