[code language=”sql”]
CREATE FUNCTION [dbo].[fn_split] (
@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]
Vinicius Fonseca
Arquiteto de Soluções e sócio @ Iteris, um desenvolvedor experiente que gosta de aprender e se esforça para ser melhor a cada dia. // Solution Architect and partner @ Iteris, a seasoned developer who loves to learn and strives to be better everyday.
#Developer #JustDoIT #AllYourBaseAreBelongToUs #Supercalifragilisticexpialidocious