CREATE FUNCTION [dbo].[CalculateAge]
(
@DOB datetime , @calcDate datetime
)
RETURNS int
AS
BEGIN
declare @age int
IF (@calcDate < @DOB )
RETURN -1
-- If a DOB is supplied after the comparison date, then return -1
SELECT @age = YEAR(@calcDate) - YEAR(@DOB) +
CASE WHEN DATEADD(year,YEAR(@calcDate) - YEAR(@DOB)
,@DOB) > @calcDate THEN -1 ELSE 0 END
RETURN @age
END
go
-- Step) 2
go
SELECT dbo.CalculateAge('1985-09-15',Getdate()) as CurrentAge
Share This with your friend by choosing any social account