create FUNCTION [development].[GetWordAt2] (@LongSentence varchar(max),@FindSubString nvarchar(100),@wordnum int)
RETURNS varchar(1000)
AS
BEGIN
Declare @Occurances int, @counter int, @currentIndex int, @previousIndex int, @result nvarchar(1000)
set @counter = 1
set @currentIndex =1
set @previousIndex = 0
set @Occurances = (LEN(@LongSentence) - LEN(REPLACE(@LongSentence, @FindSubString, '')))/LEN(@FindSubString)
While @counter < @wordnum
Begin
set @currentIndex = CHARINDEX(@FindSubString, @LongSentence,@currentIndex)+len(@FindSubString)+1
set @counter = @counter + 1
set @previousIndex=@currentIndex
End
select @result=case when (@counter > @Occurances)
then
substring(@LongSentence,@previousIndex-1,1000)
else
substring(@LongSentence,@previousIndex-1,@currentIndex-@previousIndex-1)
end
return @result
END
No comments:
Post a Comment