IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FNTU_OrdineVersione]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[FNTU_OrdineVersione]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO


CREATE FUNCTION dbo.[FNTU_OrdineVersione](
    @Versione1 varchar(20),
    @Versione2 varchar(20)
) RETURNS smallint
AS
BEGIN	
	declare @result as smallint
	if @Versione1 = @Versione2 
		set @result = 0
	else if convert(tinyint,left(@Versione1,2))<convert(tinyint,left(@Versione2,2)) 
		set @result = 1
	else if convert(tinyint,left(@Versione1,2))>convert(tinyint,left(@Versione2,2)) 
		set @result = -1		
	else if convert(tinyint,substring(@Versione1,4,2))<convert(tinyint,substring(@Versione2,4,2)) 
		set @result = 1
	else if convert(tinyint,substring(@Versione1,4,2))>convert(tinyint,substring(@Versione2,4,2)) 
		set @result = -1
	else if convert(tinyint,substring(@Versione1,7,2))<convert(tinyint,substring(@Versione2,7,2)) 
		set @result = 1
	else if convert(tinyint,substring(@Versione1,7,2))>convert(tinyint,substring(@Versione2,7,2)) 
		set @result = -1
	else
		set @result = -99	
	return @result		
end

GO


