Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Split A String By Using A Number Table
From Wiki
Using a number table is a very fast way to split a string. You can even use it inside a function if you want to. If You don't have a number table already then create this table now. Remeber this is a one time deal.
- -- Create our Pivot table ** do this only once-- populate it with 1000 rows
- CREATE TABLE NumberPivot (NumberID INT PRIMARY KEY)
- DECLARE @intLoopCounter INT
- SELECT @intLoopCounter =0
- SET NOCOUNT ON
- WHILE @intLoopCounter <=999 BEGIN
- INSERT INTO NumberPivot
- VALUES (@intLoopCounter)
- SELECT @intLoopCounter = @intLoopCounter +1
- END
- GO
Here are the queries
Numbers
- DECLARE @chvGroupNumbers VARCHAR(1000)
- SELECT @chvGroupNumbers ='1,4,77,88,4546,234,2,3,54,87,9,6,4,36,6,9,9,6,4,4,68,9,0,5,3,2,'
- SELECT SUBSTRING(',' + @chvGroupNumbers + ',', NumberID + 1,
- CHARINDEX(',', ',' + @chvGroupNumbers + ',', NumberID + 1) - NumberID -1)AS VALUE
- FROM NumberPivot
- WHERE NumberID <= LEN(',' + @chvGroupNumbers + ',') - 1
- AND SUBSTRING(',' + @chvGroupNumbers + ',', NumberID, 1) = ','
- GO
Characters
- DECLARE @chvGroupNumbers VARCHAR(1000)
- SELECT @chvGroupNumbers ='Abc,A,V,G,KJHLJJKHGGF,J,L,O,O,I,U,Y,G,V,R'
- SELECT SUBSTRING(',' + @chvGroupNumbers + ',', NumberID + 1,
- CHARINDEX(',', ',' + @chvGroupNumbers + ',', NumberID + 1) - NumberID -1)AS VALUE
- FROM NumberPivot
- WHERE NumberID <= LEN(',' + @chvGroupNumbers + ',') - 1
- AND SUBSTRING(',' + @chvGroupNumbers + ',', NumberID, 1) = ','
- GO
Order the string
- DECLARE @chvGroupNumbers VARCHAR(1000)
- SELECT @chvGroupNumbers ='Abc,A,V,G,KJHLJJKHGGF,J,L,O,O,I,U,Y,G,V,R'
- SELECT SUBSTRING(',' + @chvGroupNumbers + ',', NumberID + 1,
- CHARINDEX(',', ',' + @chvGroupNumbers + ',', NumberID + 1) - NumberID -1)AS VALUE
- FROM NumberPivot
- WHERE NumberID <= LEN(',' + @chvGroupNumbers + ',') - 1
- AND SUBSTRING(',' + @chvGroupNumbers + ',', NumberID, 1) = ','
- ORDER BY 1
- GO
Get distinct values
- DECLARE @chvGroupNumbers VARCHAR(1000)
- SELECT @chvGroupNumbers ='1,4,77,88,4546,234,2,3,54,87,9,6,4,36,6,9,9,6,4,4,68,9,0,5,3,2'
- SELECT DISTINCT SUBSTRING(',' + @chvGroupNumbers + ',', NumberID + 1,
- CHARINDEX(',', ',' + @chvGroupNumbers + ',', NumberID + 1) - NumberID -1)AS VALUE
- FROM NumberPivot
- WHERE NumberID <= LEN(',' + @chvGroupNumbers + ',') - 1
- AND SUBSTRING(',' + @chvGroupNumbers + ',', NumberID, 1) = ','
- ORDER BY 1
- GO
Contributed by: --SQLDenis 02:27, 31 May 2008 (GMT)
Part of SQL Server Programming Hacks
Section Transposing and pivoting



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.