Calculate age Using Sql server
create function CalcAge (@dob datetime)returns varchar(64)
as
begin
declare @age int
declare @getdate datetime = getdate()
if @dob >= @getdate
return 'Invalid input Parameter'
set @age = datediff(yy, @dob, @getdate)
if month(@dob) > month(@getdate) or
(month(@dob) = month(@getdate) and
day(@dob) > day(@getdate))
set @age = @age - 1
return convert(varchar,@age) + ' Years and ' + convert(varchar,datediff(dd,dateadd(yy,@age,@dob),@getdate)) + ' Days'
end
No comments:
Post a Comment