Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Tricks using a numbers table

Total Hit ( 1590)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Solving most problems using a relational technique is more elegant and better in terms of performance. This script shows few examples that uses a Numbers table to solve some common problems. Keep a table with Numbers & it will prove very handy.

Click here to copy the following block
CREATE TABLE Numbers ( Number smallint )
DECLARE @Number smallint
SELECT @Number = 0
WHILE @Number <= 1000
BEGIN
  INSERT Numbers VALUES ( @Number )
  SELECT @Number = @Number + 1
END
go

-- Examples:
-- To strip values out from a CSV string that contains only numbers
DECLARE @str varchar(255)
SELECT @Str = ',1,2,3,4,5,6,7,8,9,'
SELECT Number AS Value
FROM Numbers
WHERE CHARINDEX',' + CONVERT(varchar, Number ) + ',' , @Str ) > 0

-- To generate dates between two ranges
DECLARE @StartDate datetime, @EndDate datetime
SELECT @StartDate = '1999-10-24 00:00', @EndDate = '1999-12-01 00:00'
SELECT DATEADD( dd, Number , @StartDate ) AS IndDate
FROM Numbers
WHERE DATEADD( dd, Number , @StartDate ) <= @EndDate

-- In SQL70
SELECT @StartDate + Number AS IndDate
FROM Numbers
WHERE @StartDate + Number <= @EndDate


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.