Here is a function to compute the age of a individual, by passing Birth Date and As On Date.
CREATE FUNCTION [dbo].[fn_GetAge]
(
@DOB DATETIME ,
@AsOfDate DATETIME
)
RETURNS INT
AS
BEGIN
DECLARE @Age INT
IF @DOB >= @AsOfDate
RETURN 0
SET @Age = DATEDIFF(yy, @DOB, @AsOfDate)
IF MONTH(@DOB) > MONTH(@AsOfDate)
OR ( MONTH(@DOB) = MONTH(@AsOfDate)
AND DAY(@DOB) > DAY(@AsOfDate)
)
SET @Age = @Age – 1
RETURN @Age
END
How to Execute this Function:-
SELECT [dbo].[fn_GetAge] (’02/04/1983′,’03/09/2011′)
No comments:
Post a Comment