Technical Article

Improved Split function

,

This is a script function I've been using for years.

CREATE FUNCTION [dbo].[fSplit]
(
@List VARCHAR(6000),
@SplitOn VARCHAR(5)
)  
RETURNS @RtnValue TABLE
(

ID INT identity(1,1),
Value VARCHAR(100)
) 
AS  
BEGIN 
WHILE (Charindex(@SplitOn,@List)>0)
BEGIN  
INSERT INTO 
@RtnValue (value)
SELECT 
    Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))

SET @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
END     

INSERT INTO 
@RtnValue (Value)
    SELECT 
Value = ltrim(rtrim(@List))

    RETURN
END

Rate

1 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

1 (1)

You rated this post out of 5. Change rating