Monday, February 22, 2010

Function to return a table from a delimitted (csv) string

Create function fn_CSVString
(@CSVStr varchar(8000) ,@Delim varchar(10))
returns @CSV table (s varchar(1000))
as
begin

declare @i int , @j int
select @i = 1

while @i <= len(@CSVStr)
begin
select @j = charindex(@Delim, @CSVStr, @i)
if @j = 0
begin
select @j = len(@CSVStr) + 1
end
insert @CSV
select substring(@CSVStr, @i, @j - @i)

select @i = @j + len(@Delim)
end
Select * from @CSV
return
end

No comments:

Post a Comment