We often have need of
splitting in applications. I had need of one in some T-SQL development I am currently doing for a friend. After development and testing, I’ve decided this should be available for all dudes over the net who need this kind of functionality in their applications. So I am uploading function’s source code on my favorite site.
Simply copy and paste the following
function code in your T-SQL code editor, to select appropriate DB from database combo box and then press F5 key to create user defined
function and you can use it easily.
CREATE FUNCTION dbo.Split ( @String VARCHAR(8000), @Delimiter NVARCHAR(1))
RETURNS @Tokens table
INSERT INTO @Tokens (Token) VALUES (LTRIM(RTRIM(SUBSTRING(@String,1,CHARINDEX(@Delimiter,@String)-1))))
SET @String = SUBSTRING(@String,
INSERT INTO @Tokens (Token) VALUES (LTRIM(RTRIM(@String)))
To execute the function using the following example:
SELECT * FROM dbo.Split ('Token1;Token2;Token3;Token4;Token5',';')