SQL Split Function
Hey all,
I have used this little doozy for quite some time now. I normally use it to break up string arrays coming in from the middle ware or the front end and throw it into an easy to use table variable. Since it's a scalar function you will be able to examine each row in the "array" by value and/or index (1 based). I know you will find other genius ways to abuse this puppy...
CREATE Function fn_Split(@inString varchar(8000),@delim varchar(5))
Returns @arrSplit table(idx int Primary Key,value varchar(255))
AS
Begin
Declare @idx int,@value varchar(255)
Set @idx = 0
If len(@inString) > 0
Begin
while charindex(@delim,@inString) > 0
Begin
Set @value = Substring(@inString,1,Charindex(@delim,@inString)-1)
If ascii(@inString) <> ascii(@delim)
Begin
Insert @arrSplit(idx,value)
values(@idx,@value)
SET @idx = @idx + 1
End
Set @inString = right(@inString,(len(@inString) - (Charindex(@delim,@inString))))
End
If ascii(@inString) <> ascii(@delim)
Begin
insert @arrSplit(idx,value)
values(@idx,@inString)
End
END
Return
End
Enjoy.
I have used this little doozy for quite some time now. I normally use it to break up string arrays coming in from the middle ware or the front end and throw it into an easy to use table variable. Since it's a scalar function you will be able to examine each row in the "array" by value and/or index (1 based). I know you will find other genius ways to abuse this puppy...
CREATE Function fn_Split(@inString varchar(8000),@delim varchar(5))
Returns @arrSplit table(idx int Primary Key,value varchar(255))
AS
Begin
Declare @idx int,@value varchar(255)
Set @idx = 0
If len(@inString) > 0
Begin
while charindex(@delim,@inString) > 0
Begin
Set @value = Substring(@inString,1,Charindex(@delim,@inString)-1)
If ascii(@inString) <> ascii(@delim)
Begin
Insert @arrSplit(idx,value)
values(@idx,@value)
SET @idx = @idx + 1
End
Set @inString = right(@inString,(len(@inString) - (Charindex(@delim,@inString))))
End
If ascii(@inString) <> ascii(@delim)
Begin
insert @arrSplit(idx,value)
values(@idx,@inString)
End
END
Return
End
Enjoy.

