How to caculate Date of Age in ms sql server
Microsoft Net Framework

How to caculate Date of Age in ms sql server?


-- Step) 1 


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


Upcoming Articles
You may also read following recent Post
Copyright Future Minutes © 2015- 2024 All Rights Reserved.   Terms of Service  |   Privacy Policy |  Contact US|  Pages|  Whats new?
Update on: Dec 20 2023 05:10 PM