Monday, 5 February 2018

How to split string in sql server

create function [dbo].[splitStrs]
(
  @Str nvarchar(4000)
)
returns @val Table(sID VARCHAR(20))
As
  begin
     declare @tempval nvarchar(10)
   
      while(Len(@Str) > 0)
       begin
         set @tempval=  Left(@Str,IsNull(NullIf(charIndex(',',@Str)-1,-1) ,Len(@Str)))
         set @Str= substring(@Str,Isnull(NullIf(charIndex(',',@Str),0),Len(@Str))+1,Len(@Str))
         insert into @val values(@tempval)
       end
     return
  end
GO

No comments:

Post a Comment