posted on Friday, December 17, 2004 10:01 AM by amachanic

You REQUIRE a Numbers table!

Looking at my list of upcoming articles, I keep seeing the same theme repeated over and over. A sequence table of Numbers.

Numbers tables are truly invaluable. I use them all of the time for string manipulation, simulating window functions, populating test tables with lots of data, eliminating cursor logic, and many other tasks that would be incredibly difficult without them.

Is using a table of numbers a hack, as I've seen some people claim? No. Show me another way to efficiently do all of the things a numbers table can. Does it waste space? No. The script below will use up around 900 KB of disk space in each database. That's absolutely nothing. You'll end up getting millions, maybe billions of times the disk space investment back in terms of ease of development and time saved.

So henceforth, I will assume in this blog that everyone reading has a Numbers table. And I will politely link to this article as a gentle reminder. But I want you to open Query Analyzer right now and use the following script:

USE Model
GO

CREATE TABLE Numbers
(
Number INT NOT NULL,
CONSTRAINT PK_Numbers
PRIMARY KEY CLUSTERED (Number)
WITH FILLFACTOR = 100
)

INSERT INTO Numbers
SELECT
(a.Number * 256) + b.Number AS Number
FROM
(
SELECT number
FROM master..spt_values
WHERE
type = 'P'
AND number <= 255
) a (Number),
(
SELECT number
FROM master..spt_values
WHERE
type = 'P'
AND number <= 255
) b (Number)
GO

There. Now you automatically have a Numbers table in every database you create, populated with every number between 0 and 65535. That's big enough for most tasks. If you need more numbers, just insert more! It's fun and easy! And trust me, you'll use this table. A lot. And you'll thank me one day, probably by sending me lots of gifts, as a very small token of your appreciation.

But in the meantime, here are two links with more information on using a numbers table:

ASP FAQ #2516, "Why should I consider using an auxiliary numbers table?"

Fun with numbers in Transact-SQL queries


Update, December 1, 2005: Fixed the insert script for SQL Server 2005; master..spt_values now has a lot more numbers (0-2048 instead of 0-255) so as-is the script was failing. It should work properly now.

Comments

# Controlling Stored Procedure Caching with ... Dyanmic SQL?!? @ Friday, January 07, 2005 12:27 PM

Controlling Stored Procedure Caching with ... Dyanmic SQL?!?

amachanic

# Splitting a string of unlimited length @ Tuesday, January 11, 2005 11:23 AM

Splitting a string of unlimited length

amachanic

# Counting occurrences of a substring within a string @ Tuesday, January 11, 2005 11:50 AM

Counting occurrences of a substring within a string

amachanic

# re: You REQUIRE a Numbers table! @ Wednesday, June 08, 2005 11:26 PM

completely agree. i've been using number tables to turn varchars into streams for a while now. glad to know i am not alone in the universe

peter

# re: You REQUIRE a Numbers table! @ Thursday, June 30, 2005 10:18 AM

oh you are alone in the universe. This is all in your imagination.

Mike

# re: You REQUIRE a Numbers table! @ Friday, October 28, 2005 3:25 PM

Hey there, Adam...

I thank you again for turning me on to both the numbers (tally) table and the date table thing during one of your replies to my "Calculating Workdays between Dates" article I wrote on SQL Server Central. The numbers table, in particular, has been the magic-bullet for a lot of otherwise loop intensive RBAR (Row By AGONIZING Row).

--Jeff Moden
jbmoden@ameritech.net

Jeff Moden

# You REQUIRE a Numbers table! @ Thursday, July 13, 2006 12:19 AM

Originally posted here.

Looking at my list of upcoming articles, I keep seeing the same theme repeated...

Anonymous

# Controlling Stored Procedure Caching with ... Dyanmic SQL?!? @ Thursday, July 13, 2006 12:20 AM

Originally posted here.
Tell me if this situation sends a chill down your spine: You've written
a stored...

Anonymous

# Splitting a string of unlimited length @ Thursday, July 13, 2006 12:22 AM

Originally posted here.
There are many techniques for splitting a string in T-SQL (in other
words,...

Anonymous

# Counting occurrences of a substring within a string @ Thursday, July 13, 2006 12:23 AM

Originally posted here.

I have absolutely no idea why anyone wants to do this, but I keep
answering...

Anonymous

# Persistent Data Sequencing (Part 1 of 2) @ Wednesday, August 02, 2006 3:04 AM

Anonymous

# Counting occurrences of a substring within a string @ Monday, January 08, 2007 2:36 PM

Originally posted here . I have absolutely no idea why anyone wants to do this, but I keep answering

Anonymous

# Splitting a string of unlimited length @ Monday, January 08, 2007 2:36 PM

Originally posted here . There are many techniques for splitting a string in T-SQL (in other words, taking

Anonymous

# Controlling Stored Procedure Caching with ... Dyanmic SQL?!? @ Monday, January 08, 2007 2:36 PM

Originally posted here . Tell me if this situation sends a chill down your spine: You've written a stored

Anonymous

# You REQUIRE a Numbers table! @ Monday, January 08, 2007 2:37 PM

Originally posted here . Looking at my list of upcoming articles, I keep seeing the same theme repeated

Anonymous