Showing posts with label comma seprated values to table. Show all posts
Showing posts with label comma seprated values to table. Show all posts

Wednesday, June 26, 2013

Separate comma separated values and store in table


create function [dbo].[fncsvtotable]
(
 @csv_string varchar(max)
)

returns @output_table_tmp table(col_value varchar(100))

as

begin

 declare @value varchar(100)

 while len(@csv_string) > 0
 begin
  set @value = ltrim(left(@csv_string, isnull(nullif(charindex(',', @csv_string) - 1, -1), len(@csv_string))))
  set @csv_string = substring(@csv_string,isnull(nullif(charindex(',', @csv_string), 0),
           len(@csv_string)) + 1, len(@csv_string))
  if isnull(@value, '') <> ''
   insert into @output_table_tmp values (@value)
 end

 return
end