Welcome to SqlAdvice Sign in | Join | Help

Iterate Over a List of Dates using a Numbers table in SQL

Recently I was helping Steve archive off a bunch of data from one of his tables. Unfortunately he didn't have a ton of diskspace to use some of the techniques I normally would.  Steve recently posted a solution using powershell to do this.  I told him about an easy way to do this using a cursor and a numbers table and he told me to blog it.  This is an excellent use of a numbers table which if you don't have one of these in your database, you should create one. 

Here is a script to create a numbers table.

CREATE TABLE dbo.Numbers ( Number int IDENTITY(1, 1) PRIMARY KEY ) GO declare @MaxNumber int set @MaxNumber = 65535 WHILE 1 = 1 BEGIN INSERT INTO dbo.Numbers DEFAULT VALUES IF scope_identity() = @MaxNumber BEGIN BREAK END END GO

So basically Steve wanted to delete all data out of a table one day at a time for a date range and then shrink down the log to avoid .  The easiest way I could think of to do this is to create a cursor of all the dates in that date range and then loop through them.  Notice the join to the number table for all Numbers between 0 and the datediff of the start and end date.  Here is the code...

declare @startdate datetime declare @enddate datetime -- Declare the dates set @startdate = '01/01/2007' set @enddate = '10/01/2007' declare @period datetime declare datecursor cursor forward_only for Select dateadd(d,Number,@startdate) from Numbers where Number <= datediff(d, @startdate, @enddate) order by Number open datecursor while (1=1) begin fetch next from datecursor into @period if @@fetch_status <> 0 break; DELETE stats WHERE period = @Period BACKUP LOG stats WITH NO_LOG DBCC SHRINKFILE (stats_log, 2) end close datecursor deallocate datecursor

Here is a link to a good post on other uses of a numbers table.  As always feel free to comment on any improvements or ask any questions.

Sponsor
Published Wednesday, September 19, 2007 9:40 AM by gstark

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Tuesday, September 25, 2007 6:46 AM by KenPem

# re: Iterate Over a List of Dates using a Numbers table in SQL

Love the idea of a numbers table, gonna poach that!

You and I obviously went to different schools of programming though - I'm allergic to things like never-ending loops escaped with a BREAK. But I guess that's just a personal semantic preference!

Nice one.

Tuesday, September 25, 2007 8:55 AM by gstark

# re: Iterate Over a List of Dates using a Numbers table in SQL

The only time I go with the never ending loop is on cursors to avoid having to write two fetch statements...See my post http://sqladvice.com/blogs/gstark/archive/2007/07/18/Sql-Server-Cursor-Template.aspx for more info.

Thursday, September 27, 2007 8:11 AM by Gregg Stark on SQL Server

# Archive and Compress Data from SQL Server

I recently blogged about deleting off data from a table by looping through one day at a time. Strangely

Wednesday, October 10, 2007 10:31 AM by Orcs Goblins and .NET

# Using a Numbers Table to Iterate Over Dates

I recently wrote some SQL which would do some work for a single day, but I wanted my code to be able

Wednesday, October 31, 2007 9:29 AM by Saggi Neumann

# tip on populating numbers tables or a "virtual" numbers table

Hi,

I usually use something along the lines of this query to fill in a numbers table (or if i just need a temp numbers table):

DECLARE @n INT

SELECT @n = 10000

SELECT TOP (@n) ROW_NUMBER() OVER (ORDER BY a.id)

INTO #num

FROM master..sysobjects a, master..sysobjects b

It's probably faster than inserting numbers one by one. In SQL 2000 I'd use IDENTITY over a cross join (max. number in this cross join is over 3 million which would suffice)

Enjoy!

Wednesday, May 06, 2009 2:18 PM by Brendan Enrick's Blog

# Using a Numbers Table to Iterate Over Dates

I recently wrote some SQL which would do some work for a single day, but I wanted my code to be able to run once for each day in a date range. Luckily for me, I read Gregg Stark's SQL Blog . He recently posted about how to Iterate Over a List of Dates

Monday, July 26, 2010 3:22 PM by Brendan Enrick

# Using a Numbers Table to Iterate Over Dates

Using a Numbers Table to Iterate Over Dates

Friday, July 30, 2010 9:50 AM by Brendan Enrick

# Using a Numbers Table to Iterate Over Dates

Using a Numbers Table to Iterate Over Dates

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below