If OBJECT_ID('dbo.Extension') IS NOT NULL DROP FUNCTION dbo.Extension
GO
CREATE FUNCTION dbo.Extension(@cFile VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
	DECLARE @cExtension VARCHAR(MAX)
	DECLARE @offset INT
	SET @cExtension='(nvt)'
	SET @offset=CHARINDEX('\',@cFile,0)
	WHILE @offset>0
		BEGIN
			SET @cFile=SUBSTRING(@cFile,@offset+1,LEN(@cFile)-@Offset)
			SET @offset=CHARINDEX('\',@cFile,0)
		END
	
	SET @offset=CHARINDEX('.',@cFile,0)
	IF @offset>0
		BEGIN
			SET @cExtension=SUBSTRING(@cFile,@offset+1,LEN(@cFile)-@Offset)
			SET @offset=CHARINDEX('.',@cExtension,0)
			WHILE @offset>0
				BEGIN
					SET @cExtension=SUBSTRING(@cExtension,@offset+1,LEN(@cExtension)-@Offset)
					SET @offset=CHARINDEX('.',@cExtension,0)
				END
		END
	RETURN @cExtension
END
GO