-- Usage : SELECT * FROM SplitString('1,2,3', ',')
-- Result :
-- 1
-- 2
-- 3
CREATE FUNCTION [dbo].[SplitString](@String varchar(max), @Delimiter varchar(100))
RETURNS @Results TABLE (Items varchar(max))
AS
BEGIN
DECLARE @INDEX INT
DECLARE @SLICE varchar(max)
SELECT @INDEX = 1
IF @String IS NULL RETURN
WHILE @INDEX !=0
BEGIN
SELECT @INDEX = CHARINDEX(@Delimiter,@STRING)
IF @INDEX !=0
SELECT @SLICE = LEFT(@STRING,@INDEX - 1)
ELSE
SELECT @SLICE = @STRING
INSERT INTO @Results(Items) VALUES(@SLICE)
SELECT @STRING = RIGHT(@STRING,LEN(@STRING) - @INDEX)
IF LEN(@STRING) = 0 BREAK
END
RETURN
END
Returns split values from a character separated string
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.