Select records from a database that have changed within the last 30 days

© copyright 07.Jan.2009 by Paul Bradley filed under Microsoft SQL Server


[Ad] need a break from coding?

Hoseasons Villas

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())

 


If you have found this article helpful or useful please consider linking to it, emailing it to friends, or share it with others using social sites like del.icio.us, Stumble Upon or Twitter.

Paul Bradley

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.

Other Popular Articles

Categories & Topics

Home · Apache · JavaScript · Perl · PDF · PHP · MySQL · MSSQL · TAR · Ubuntu Linux · Video · Visual Basic

Browse the complete article history, and if you like what you see; consider subscribing to the rss feed.