Friday, July 18, 2008

SQL Split Function

CREATE FUNCTION Split
(
@Word VARCHAR(8000),
@Separator VARCHAR(255)
)
RETURNS @SplitKeyword TABLE (Keyword VARCHAR(8000))
AS
BEGIN
DECLARE @TempWord VARCHAR(255)
DECLARE @TempKeyword TABLE (Keyword VARCHAR(8000))

WHILE (CHARINDEX(@Separator, @Word, 1)>0)
BEGIN
SET @TempWord = SUBSTRING(@Word, 1 , CHARINDEX(@Separator, @Word, 1) - 1)
SET @Word = SUBSTRING(@Word, CHARINDEX(@Separator, @Word, 1) + 1, LEN(@Word))

INSERT INTO @TempKeyword VALUES(@TempWord)
END

INSERT INTO @TempKeyword VALUES(@Word)

INSERT @SplitKeyword
SELECT * FROM @TempKeyword
RETURN
END

SELECT * FROM Split('10/31','/')

No comments:

Site Meter