String.LastIndexOf, but in T-SQL (handy for getting file extensions)

Note that this doesn’t work on the “text” datatype, so you’ll need to cast to nvarchar…


SELECT LEN(FileName) - CHARINDEX('.', REVERSE(FileName)) AS ‘LastIndex’,
RIGHT(FileName, CHARINDEX('.', REVERSE(FileName))) AS ‘FileExtension’ – still includes the dot
FROM Files