Return to Snippet

Revision: 10054
at December 5, 2008 01:09 by robe


Initial Code
--- takes a delimetered value and returns a table 
--Usage:
-- SELECT Data
-- FROM dbo.fnSplit('A,B,C', ',');

CREATE FUNCTION [dbo].[fnSplit]
(
	@RowData varchar(4000),
	@SplitOn varchar(5)
)  
RETURNS @RtnValue table 
(
	Id int identity(1,1),
	Data varchar(1000)
) 
AS  
	BEGIN 
		Declare @Cnt int
		Set @Cnt = 1

		While (Charindex(@SplitOn,@RowData)>0)
		Begin
			Insert Into @RtnValue (data)
			Select 
				Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1)))

			Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData))
			Set @Cnt = @Cnt + 1
		End
		
		Insert Into @RtnValue (data)
		Select Data = ltrim(rtrim(@RowData))

		Return
	END

Initial URL

                                

Initial Description

                                

Initial Title
SQL Server Split function

Initial Tags

                                

Initial Language
SQL