The function below returns the position of the last occurrence of
@char
in
@s
. If
@char
does not exist in
@s
or
@s
is empty, 0 is returned. (First place in string is 1)
Create function GetLastCharIndex(@s varchar(max), @char varchar(200)) returns int
as
begin
DECLARE @lastIndex int
DECLARE @nOccurance int
DECLARE @searchExpression varchar(max)
SET @nOccurance = len(@s)
SET @lastIndex = 0
SET @searchExpression = @s
while @nOccurance > 0
BEGIN
SELECT @nOccurance = charIndex(@char, @searchExpression)
IF (@nOccurance > 0)
BEGIN
SET @lastIndex = @lastIndex + @nOccurance
SET @searchExpression = substring(@searchExpression, @nOccurance + 1, len(@searchExpression))
END
END
return @lastIndex
end