THE SQL Server Blog Spot on the Web

Welcome to - The SQL Server blog spot on the web Sign in | |
in Search

Peter DeBetta's SQL Programming Blog

Peter DeBetta's blog about programming in SQL Server using technologies such as T-SQL, .NET, CLR, C#, VB, Visual Studio, and SQL Server Management Studio.

Delimited List to Table - Follow Up

Enigma ( posted a comment on the first entry in this topic regarding another method for converting a delimited list to a table. I will show an example of it here (formatted for ease of reading):

Given the structures...

CREATE TABLE Quotes (Author Varchar(30), Phrase Varchar(1000))

...where table Tally is a sequential list of numbers from 1 to 8000 (you can use this script to populate it)...

SET @a = 1
WHILE (@a <=8000)
 SET @a = @a + 1

...and where Quotes has, let's say, the following data...

INSERT INTO Quotes VALUES('Shakespeare', 'A,rose,by,any,other,name,smells,just,as,sweet')

Then the following query would return each delimited value from the Phrase column as a separate row in the resulting table...

SELECT  NullIf(SubString(',' + Phrase + ',' , ID , CharIndex(',' , ',' + Phrase + ',' , ID) - ID) , '')
          AS Word
FROM  Tally
  ON ID <= Len(',' + Phrase + ',')
   AND  SubString(',' + Phrase + ',' , ID - 1, 1) = ','
   AND  CharIndex(',' , ',' + Phrase + ',' , ID) - ID > 0
WHERE  Author = 'Shakespeare'

Wow! Many thanks to Joe Celko for this beauty. I love how this works. This is the beauty of set-based operations. For a full explanation of how this works, go to

Now, how would we apply this to our situation? Well, I want a UDF that will accept any delimited list and delimiter using join technique shown here. Details will be in the next post...

Published Monday, June 26, 2006 12:59 PM by Peter W. DeBetta


No Comments
New Comments to this post are disabled

About Peter W. DeBetta

Peter DeBetta works as a Data and Cloud Solution Manager at DB Best, a Microsoft gold partner company. Peter has written courseware, articles, and books on topics such as development and SQL Server. Peter has spoken at conferences around the world, including TechEd, SQL PASS Community Summit, DevTeach, SQL Connections, DevWeek, and VSLive!

When Peter isn’t working, you can find him singing and playing guitar, taking pictures, working on DIY projects, woodworking, or simply enjoying life with his kids.
Privacy Statement