Wednesday, August 28, 2013

CSV to INT

CSV to INT

this is how you can convert a CVS to Int

CREATE function [dbo].[FN_CsvToInt] (@id varchar(5000))      
returns @table table (wd_code int identity(1,1), [id] int)     
as     
begin     
     
 declare @separator char(1)     
 set @separator = ','     
     
 declare @separator_position int      
 declare @array_value varchar(5000)      
      
 set @id = @id + ','     
      
 while patindex('%,%' , @id) <> 0      
 begin     
      
   select @separator_position =  patindex('%,%' , @id)     
   select @array_value = left(@id, @separator_position - 1)     
      
 if(@array_value = 'null')     
  insert @table ([id])     
  values (null)     
     
 else     
  insert @table ([id])     
  values (cast(@array_value as int))     
     
   select @id = stuff(@id, 1, @separator_position, '')     
 end     
     
 return     
end


Regards,
Sheryar Nizar

No comments: