By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
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
SET NOCOUNT ON
DECLARE @Item Varchar(8000)
CREATE TABLE #List(Item varchar(8000)) -- Create a temporary table
--IF CHARINDEX(@Delimiter,@sInputList,0) <> 0
WHILE CHARINDEX(@Delimiter,@sInputList,0) <> 0
IF LEN(@Item) > 0
INSERT INTO #List SELECT @Item
IF LEN(@sInputList) > 0
INSERT INTO #List SELECT @sInputList -- Put the last item in
SELECT * FROM #List
DROP TABLE #List
To test the procedure:
EXEC sp_Txt_Split '1,22,333,444,,5555,666', ','
EXEC sp_Txt_Split '1#22#333#','#'
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))
DECLARE @sItem VARCHAR(8000)
WHILE CHARINDEX(@sDelimiter,@sInputList,0) <> 0
IF LEN(@sItem) > 0
INSERT INTO @List SELECT @sItem
IF LEN(@sInputList)> 0
INSERT INTO @List SELECT @sInputList -- Put the last item in
select * from fnSplit('1,22,333,444,,5555,666', ',')
select * from fnSplit('1##22#333##444','##') --note second item has
select * from fnSplit('1 22 333 444 5555 666', ' ')
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.