Função Split para SQL Server em T-SQL
@Text VARCHAR(MAX),
@Separator VARCHAR(10)
)
RETURNS @Table TABLE (Token VARCHAR(MAX))
AS
BEGIN
DECLARE @StartPosition INT;
DECLARE @EndPosition INT;
SET @StartPosition = 1;
SET @EndPosition = CHARINDEX(@Separator, @Text, @StartPosition);
WHILE (@EndPosition > 0)
BEGIN
INSERT INTO @Table (Token) VALUES (SUBSTRING(@Text, @StartPosition, @EndPosition – @StartPosition));
SET @StartPosition = @EndPosition + LEN(@Separator);
SET @EndPosition = CHARINDEX(@Separator, @Text, @StartPosition);
END; — WHILE
INSERT INTO @Table (Token) VALUES (SUBSTRING(@Text, @StartPosition, LEN(@Text) – @StartPosition + 1));
RETURN;
END;
[/code]