Posts Tagged ‘last index’

h1

Get LASTINDEX in SQL

June 28, 2012

First create a function as folloing.

CREATE FUNCTION
CREATE FUNCTION dbo.LAST_INDEX(@STRING VARCHAR(8000), @CHAR CHAR)
RETURNS INT
AS
BEGIN
RETURN LEN(@STRING) – CHARINDEX(@CHAR, REVERSE(@STRING), 1 + 1)
END

Now you can use this like

SELECT LAST_INDEX(‘TEST.STRING’,’.’)

 

Advertisements