You are on page 1of 1

C:\Documents and Settings\gardg900\Local Settings\Temp\~vs570.

sql

1 USE [master] 2 GO 3 /****** Object: UserDefinedFunction [dbo].[fn_ReturnSplit] Script Date: 10/06/2010 17 :52:33 ******/ 4 SET ANSI_NULLS OFF 5 GO 6 SET QUOTED_IDENTIFIER OFF 7 GO 8 9 ALTER FUNCTION [dbo].[fn_ReturnSplit](@text varchar(8000), @delimiter varchar(20) = ' ', @Position int) 10 RETURNS varchar(8000) 11 AS 12 BEGIN 13 Declare @Strings TABLE 14 ( 15 position int IDENTITY PRIMARY KEY, 16 value varchar(8000) ) 17 DECLARE @ReturnString varchar (8000) 18 DECLARE @index int 19 SET @index = -1 20 IF @delimiter = ' ' 21 BEGIN 22 SET @text = ltrim(rtrim(@text)) 23 END 24 25 WHILE (LEN(@text) > 0) 26 BEGIN 27 SET @index = CHARINDEX(@delimiter , @text) 28 IF (@index = 0) AND (LEN(@text) > 0) 29 BEGIN 30 INSERT INTO @Strings VALUES (@text) 31 BREAK 32 END 33 IF (@index > 1) 34 BEGIN 35 INSERT INTO @Strings VALUES (LEFT(@text, @index - 1)) 36 SET @text = RIGHT(@text, (LEN(@text) - @index)) 37 END 38 ELSE 39 SET @text = RIGHT(@text, (LEN(@text) - @index)) 40 END 41 set @ReturnString = (select top 1 value from @Strings where position = @Position ) 42 RETURN @ReturnString 43 END 44 45