Rudy Rodarte . com

Information

This article was written on 27 Dec 2017, and is filled under SQL Blogs.

Current post is tagged

, ,

Loop over dates in SQL Server

I’ve written the script below at least once a month, for either reports, or working with large data sets. Rather than reinventing the wheel, I’m posting my code here and to GitHub for everyone to see.

The script expects two date parameters, a start date and an overall end date. The script will then assign a third variable, end date, seven days after the start date. The script will execute your given code. The final step inside the loop increments the start date and end date by seven days. The script will loop until the start date is greater then the overall end date.

As you can see, the loop iterates over all dates, and completes successfully.

Follow this project on GitHub:

Rudy

Rudy Rodarte is a SQL Server Database professional based in Austin, TX. Over his career, Rudy has worked with SSRS, SSIS, performance tuning troubleshooting. When away from Keyboard, Rudy goes to Spurs and Baylor Bear sporting events.

More Posts - Website - Twitter