Thursday 27 February 2020

Spilit comma seprated string using SQL Server 2012

Create a function that will separate the comma-separated string and extract each word in using SQL Server 2012.

declare @str varchar(50),@l int,@i int,@p int,@slen int
select @str='o,222,333,kl,4444,555,88pol'
select @l=LEN(@str)
set @i=1
set @p=0
set @slen=1

while @i<=@l
begin
       if SUBSTRING(@str,@i,1)=','
       begin
              select substring(@str,@p+1,@i-@p-1),@p,@i
              set @p=@i
       end
       set @i=@i+1

end

select substring(@str,@p+1,@l-@p),@p,@i

No comments:

Post a Comment