if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetAge]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetAge]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE FUNCTION [dbo].[GetAge] (@D1 datetime,@D2 datetime)  
RETURNS int AS  
BEGIN 
	declare @Y int,@TempD datetime
	if @D1 > @D2
		set @Y = 0
	else
	begin
		set @Y = 0
		set @TempD = dateadd(year,@Y,@D1)
		while @TempD <= @D2
		begin
			set @Y = @Y + 1
			set @TempD = dateadd(year,@Y,@D1)
		end
		set @Y = @Y - 1
	end
	RETURN @Y 
END


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GRANT  EXECUTE  ON [dbo].[GetAge]  TO [public]
GO

