© copyright 07.Jan.2009 by Paul Bradley filed under Microsoft SQL Server
It's useful to be able to quickly retrieve records from a database that have changed in the last 7, 14 or 30 days. Lets suppose you have an orders header table with a field for order date, and you want to retrieve a list of all the order numbers issued with the last 30 days, you can use the following SQL syntax.
The GETDATE function returns the current system date, and then we ADD minus 30 days to it which will give us a date 30 days into the past; which is then used as a comparison against the order_date field.
SELECT order_number FROM order_header
WHERE order_date >= DATEADD(d, -30, GETDATE())
About the Author
Paul Bradley is a VB.NET software developer living and working in Cumbria. He provides PHP & MySQL bespoke development services via his software development company, Carlisle Software Limited.
He has over 20 years programming experience.