Problem solve Get help with specific problems with your technologies, process and projects.

Using split delimiter-separated lists in SQL Server

This SQL Server function is useful to split a string of values delimited by a special character.

View member feedback to this tip.

This function is useful for splitting a string of values delimited by a special character; e.g., a comma. I recently had a case where the list of employees for a department needed to be inserted into a table. The employee names were in a string delimited by commas. Separating these names in Visual Basic and inserting them into the database involved many database hits. Instead, I passed the string to the following stored procedure. The stored procedure split the string and inserted each employee name along with department name into the database.

This script has been tested on SQL server 7.0. Note that it takes ' '(space) also as a delimiter.

 CREATE PROCEDURE dbo.sp_Txt_Split(
   @sInputList varchar(8000) -- List of delimited items
   , @Delimiter char(1) = ',' -- delimiter that separates items
)
AS BEGIN
SET NOCOUNT ON
DECLARE @Item Varchar(8000)
CREATE TABLE #List(Item varchar(8000)) -- Create a temporary table
--IF CHARINDEX(@Delimiter,@sInputList,0) <> 0
--BEGIN
WHILE CHARINDEX(@Delimiter,@sInputList,0) <> 0
BEGIN
SELECT
@Item=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@Delimiter,
   @sInputList,0 )-1))),
@sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@Delimiter,
   @sInputLi st,0)+1,LEN(@sInputList))))
IF LEN(@Item) > 0
INSERT INTO #List SELECT @Item
END
--END
IF LEN(@sInputList) > 0
INSERT INTO #List SELECT @sInputList -- Put the last item in
SELECT * FROM #List
DROP TABLE #List
RETURN
END
GO

To test the procedure:

 EXEC sp_Txt_Split '1,22,333,444,,5555,666', ','

or

 EXEC sp_Txt_Split '1#22#333#','#'

 


MEMBER FEEDBACK TO THIS TIP

I decided to implement the process in this tip as a function.

The benefits of this method are that it doesn't use tempdb (so it won't be affected by anything that may lock tempdb), it is slightly faster, and you can use the function just like any other table -- for example, select c.* from customers c where c.city in (select item from fnSplit('Dallas,New York,Chicago',',') )

Here's the code:

 CREATE FUNCTION dbo.fnSplit(
   @sInputList VARCHAR(8000) -- List of delimited items
   , @sDelimiter VARCHAR(8000) = ',' -- delimiter that separates items
) RETURNS @List TABLE (item VARCHAR(8000))
BEGIN
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
BEGIN
SELECT
@sItem=RTRIM(LTRIM(SUBSTRING(@sInputList,1,CHARINDEX(@sDelimiter,
   @sInputList,0)-1))),
@sInputList=RTRIM(LTRIM(SUBSTRING(@sInputList,CHARINDEX(@sDelimiter,
   @sInputList,0)+LEN(@sDelimiter),LEN(@sInputList))))
IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
END
IF LEN(@sInputList)> 0
INSERT INTO @List SELECT @sInputList -- Put the last item in
RETURN
END
GO
--Test
select * from fnSplit('1,22,333,444,,5555,666', ',')
select * from fnSplit('1##22#333##444','##') --note second item has
   embedded #
select * from fnSplit('1 22 333 444 5555 666', ' ')

—David B.

 


For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free SQL Server tips and scripts.
  • Tip contest: Have a SQL Server tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, SQL Server, DB2, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical SQL Server questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: SQL Server tips, tutorials, and scripts from around the Web.


 

This was last published in September 2003

Dig Deeper on SQL Server Stored Procedures

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchBusinessAnalytics

SearchDataCenter

SearchDataManagement

SearchAWS

SearchOracle

SearchContentManagement

SearchWindowsServer

Close