Added line is this color
Deleted line is this color
+<pre> +Yet another version of the function to return a table from a delimitted string of values<br> +<br> +<br> +if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fn_ParseCSVString]') and xtype in (N'FN', N'IF', N'TF'))<br> +drop function [dbo].[fn_ParseCSVString]<br> +GO<br> +<br> +<br> +create function fn_ParseCSVString<br> +(<br> +@CSVString varchar(8000) ,<br> +@Delimiter varchar(10)<br> +)<br> +returns @tbl table (s varchar(1000))<br> +as<br> +/*<br> +select * from dbo.fn_ParseCSVString ('qwe,c,rew,c,wer', ',c,')<br> +*/<br> +begin<br> +declare @i int ,<br> + @j int<br> + select @i = 1<br> + while @i <= len(@CSVString)<br> + begin<br> + select @j = charindex(@Delimiter, @CSVString, @i)<br> + if @j = 0<br> + begin<br> + select @j = len(@CSVString) + 1<br> + end<br> + insert @tbl select substring(@CSVString, @i, @j - @i)<br> + select @i = @j + len(@Delimiter)<br> + end<br> + return<br> +end +</pre>