Create Function Dbo.Part
(@Value Varchar(8000)
,@Part Int
,@Sep Char(1)
)Returns Varchar(8000)
As Begin
Declare @Start Int
Declare @Finish Int
Set @Start=1
Set @Finish=CharIndex(@Sep,@Value,@Start)
While (@Part>1 And @Finish>0)Begin
Set @Start=@Finish+1
Set @Finish=CharIndex(@Sep,@Value,@Start)
Set @Part=@Part-1
End
If @Part>1 Set @Start=Len(@Value)+1 -- Not found
If @Finish=0 Set @Finish=Len(@Value)+1 -- Last token on line
Return SubString(@Value,@Start,@Finish-@Start)
End
Go
/*
Execute Example
Declare @Name Varchar(100)='Thillai,raja,24,6'
Select
Dbo.Part(@Name,1,',')As Name
,Dbo.Part(@Name,2,',')As Name1
,Dbo.Part(@Name,3,',')As Name2
Example Image: