Read more: http://www.blogsmonetize.com/2010/10/how-to-use-syntax-highlighter-3083-in.html#ixzz1DHzvEgBA

Saturday, August 16, 2008

Split function to get values from Comma separated string

Split function is one of the main string function to split a list of items separated by a character in our daily functions.

1. Using While loop


ALTER FUNCTION [dbo].[Split]
( @List nvarchar(2000),
@SplitOn nvarchar(5)
)
RETURNS @RtnValue table
( Id int identity(1,1),
Value nvarchar(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

No comments: