I need to select all rows in my database that were created last month.
For example, if the current month is January, then I want to return all rows that were created in December, if the month is February, then I want to return all rows that were created in January. I have a date_created
column in my database that lists the date created in this format: 2007-06-05 14:50:17
.
In SQL Server, you can use the DATEADD() function to get last 3 months (or n months) records.
SELECT * FROM product WHERE pdate >= DATEADD(day, -30, getdate()).
SELECT * FROM table WHERE YEAR(date_created) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND MONTH(date_created) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
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