Click here to Skip to main content
15,894,405 members
Articles / Programming Languages / T-SQL
Tip/Trick

Split a String with Specific Separator Character

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
18 Jun 2014CPOL 8.8K   2   2
T-SQL scripts: Write a table function to split a string with specific separator character

Introduction

Write a table function to split a string with specific separator character.

Example:

SQL
SELECT * FROM dbo.Split('john, peter, mary', ',')

And result:

  • john
  • peter
  • mary
SQL
CREATE FUNCTION [dbo].[Split](@array NVARCHAR(MAX),@separator char(1))
RETURNS @tbl TABLE (string nvarchar(128) NOT NULL) AS
BEGIN
      DECLARE @pos      int,
              @textpos  int,
              @chunklen smallint,
              @str      nvarchar(MAX),
              @tmpstr   nvarchar(MAX),
              @leftover nvarchar(MAX)

      SET @textpos = 1
      SET @leftover = ''
      WHILE @textpos <= datalength(@array) / 2
      BEGIN
         SET @chunklen = 4000 - datalength(@leftover) / 2
         SET @tmpstr = ltrim(@leftover + substring(@array, @textpos, @chunklen))
         SET @textpos = @textpos + @chunklen

         SET @pos = charindex(@separator, @tmpstr)
         WHILE @pos > 0
         BEGIN
            SET @str = substring(@tmpstr, 1, @pos - 1)
            INSERT @tbl (string) VALUES(convert(nvarchar(20), @str))
            SET @tmpstr = ltrim(substring(@tmpstr, @pos + 1, len(@tmpstr)))
            SET @pos = charindex(@separator, @tmpstr)
         END

         SET @leftover = @tmpstr
      END

      IF ltrim(rtrim(@leftover)) <> ''
         INSERT @tbl (string) VALUES(convert(nvarchar(Max), @leftover))

      RETURN
   END
GO

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Writer Softech
Vietnam Vietnam
/* Life runs on code */

Comments and Discussions

 
SuggestionEasy way to split string Pin
Member 107271924-Jul-14 0:15
Member 107271924-Jul-14 0:15 
SuggestionI have something similar . Check if the below code is works for you. Pin
Raju_B19-Jun-14 4:16
Raju_B19-Jun-14 4:16 
I have something simmler. Check if the below code is works for you.

SQL
IF  NOT EXISTS (SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[fnSplitStringToTable]') AND TYPE in (N'TF', N'PC'))
	BEGIN EXEC('CREATE FUNCTION [dbo].[fnSplitStringToTable](@StringValue	VARCHAR(8000),@SplitChar	CHAR(1)) 
				RETURNS @tblItems TABLE(Fields VARCHAR(20)) AS BEGIN RETURN END') END
GO

ALTER FUNCTION [dbo].[fnSplitStringToTable] 
(
	  @StringValue	VARCHAR(8000),
	  @SplitChar		CHAR(1)
)
RETURNS @tblItems TABLE(Fields VARCHAR(20))
AS 
BEGIN  
	DECLARE @index	INT
	DECLARE @SplitValue	VARCHAR(20)

	WHILE LEN(@StringValue)>0
		BEGIN
			SET @index=CHARINDEX(@SplitChar, @StringValue,0)
			if @index>0
				BEGIN
					SET @SplitValue = SUBSTRING(@StringValue, 0, @index)
					SET @StringValue = SUBSTRING(@StringValue, LEN(@SplitValue) + 2, LEN(@StringValue) - LEN(@SplitValue) + 1) 
					INSERT INTO @tblItems (Fields) VALUES (@SplitValue)
				END
			ELSE
				BEGIN
					INSERT INTO @tblItems (Fields) VALUES (@StringValue)
					SET @StringValue=''
				END
		END
	RETURN
END  
GO

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.