IFexists (SELECT*from dbo.sysobjects where id =object_id(N'[dbo].[fnSplitByDelim]') and xtype in (N'FN', N'IF', N'TF')) dropfunction[dbo].[fnSplitByDelim] GO /**//****** Object: UserDefinedFunction [dbo].[fnSplitByDelim] Script Date: 05/25/2006 20:22:20 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATEFUNCTION[dbo].[fnSplitByDelim] ( @SourceStringVARCHAR(max), @DelimCHAR(1) ) RETURNS@ResultTABLE(SplittedValue varchar(255), Ident INTIDENTITYnotnull) BEGIN -- ============================================================================================ -- -- FinTool -- -- ============================================================================================ -- /**//*********************************************************************************** ** Desc: Function used to return the values from string with seperator ** ** ** ** ** ** Note: ** ** ** Called By: Stored Procedures ** ** ** Author: v-svarad ** Date: Oct 1, 2006 ************************************************************************************ ** Change History ************************************************************************************ ** Date Author Description ** ---------- --------- ---------------------------------------------------------- ** 10-01-2006 v-svarad Created ***********************************************************************************/ DECLARE@Wordstartint, @WordENDint, @DoubleDelimvarchar(2) SET@Wordstart=1 SET@WordEND=1 SET@DoubleDelim=REPLICATE(@Delim, 2) -- Prepare the data WHILECHARINDEX(@DoubleDelim, @SourceString) >0 SET@SourceString=REPLACE(@SourceString, @DoubleDelim, @Delim) -- Left-trim it IFleft(@SourceString, 1) =@Delim BEGIN IF@Delim='' SET@SourceString=LTRIM(@SourceString) ELSE SET@SourceString=SUBSTRING( @SourceString, PATINDEX('%[^'+@Delim+']%', @SourceString), LEN(@SourceString) -PATINDEX('%[^'+@Delim+']%', @SourceString) +1) END -- Right-pad it IFright(@SourceString, 1) <>@Delim SET@SourceString=@SourceString+@Delim WHILE@Wordstart>0 BEGIN -- Extract the next word SET@WordEND=CHARINDEX(@Delim, @SourceString, @Wordstart) IF@WordEND>@WordStart BEGIN IFLEN(LTRIM(RTRIM(SUBSTRING(@SourceString, @Wordstart, @WordEND-@Wordstart)))) >0 INSERTinto@ResultSELECTSUBSTRING(@SourceString, @Wordstart, @WordEND-@Wordstart) SET@Wordstart=@WordEND+1 END ELSESET@Wordstart=0-- Terminate the loop END RETURN END GO GRANTSELECTON DBO.[fnSplitByDelim]TOPUBLIC GO