create
function fx_split(@stringToSplit VARCHAR(MAX), @splitString varchar(5) )
RETURNS
@returnList TABLE ([Name] [nvarchar] (500))
AS
BEGIN
DECLARE
@name NVARCHAR(255)
DECLARE @pos INT
WHILE CHARINDEX(@splitString, @stringToSplit) > 0
BEGIN
SELECT @pos = CHARINDEX(@splitString, @stringToSplit)
SELECT @name = SUBSTRING(@stringToSplit, 1, @pos-1)
INSERT INTO @returnList
SELECT @name
SELECT @stringToSplit = ltrim(rtrim(SUBSTRING(@stringToSplit, @pos+len(@splitString), LEN(@stringToSplit)-@pos)))
END
INSERT INTO @returnList
SELECT @stringToSplit
RETURN
END
Here The Use of the Function
select
* from fx_split('1,2,3,4,5', ',')
And This Is The RESULT.
1
|
2
|
3
|
4
|
5
|
My Two Cents ...