Thursday 13 December 2012

Split in mssql with specific character.


After long search on web i found some help and created this function. Its function is used for split a string in mssql.
Its return table of split word.


/****** Object:  UserDefinedFunction [dbo].[Split]   
            Script Date: 12/13/2012 07:04:16
            Created by :- Nilesh Makavana
            ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[Split]
(
   @List nvarchar(2000),
   @SplitOn nvarchar(5)
) 
RETURNS @RtnValue table
(
      Id int identity(1,1),
      Value nvarchar(100)
)
AS 
BEGIN
While (Charindex(@SplitOn,@List)>0)
Begin
      Insert Into @RtnValue (value)
      Select Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
      Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
End
Insert Into @RtnValue (Value)
Select Value = ltrim(rtrim(@List))
Return
End
GO


No comments:

Post a Comment