October 24, 2013 at 12:01 pm
How could I write a sql statement to where I could get an age in one field based off typing a date in another field?
I am trying to get age(years, months, weeks) field to populate once I type in a date in another field. Is that possible?
October 24, 2013 at 12:20 pm
declare @t table ( birthdate date, age as datediff(year,birthdate,getdate()) ) ;
insert into @t (birthdate) values ('20000101')
select * from @t
October 24, 2013 at 4:22 pm
Bill Talada (10/24/2013)
declare @t table ( birthdate date, age as datediff(year,birthdate,getdate()) ) ;insert into @t (birthdate) values ('20000101')
select * from @t
I think this will do the trick to get someone's age in years:
declare @t table ( birthdate date, age as datediff(year,birthdate,getdate())-
case when getdate()<dateadd(yy,datediff(yy,birthdate,getdate()), birthdate) then 1 else 0 end );
insert into @t (birthdate) values ('20000101')
select * from @t
Lynn Pettis published a great article on this topic: Calculating Age[/url]
Edit: Typo
-- Itzik Ben-Gan 2001
October 24, 2013 at 4:47 pm
todd.ayers (10/24/2013)
How could I write a sql statement to where I could get an age in one field based off typing a date in another field?I am trying to get age(years, months, weeks) field to populate once I type in a date in another field. Is that possible?
To get age in years, month, weeks you could do this:
declare @t table
(birthdate date,
age_yrs as datediff(year,birthdate,getdate())-
case
when getdate()<dateadd(yy,datediff(yy,birthdate,getdate()), birthdate)
then 1
else 0
end,
age_mos as (datediff(mm,birthdate,getdate())-
case
when getdate()<dateadd(mm,datediff(mm,birthdate,getdate()), birthdate)
then 1
else 0
end)%12,
age_wks as (datediff(week,birthdate,getdate())-
case
when getdate()<dateadd(week,datediff(week,birthdate,getdate()), birthdate)
then 1
else 0
end)%52);
insert into @t (birthdate) values ('20121015')
select * from @t
-- Itzik Ben-Gan 2001
October 24, 2013 at 6:07 pm
It is Never a good idea to store the age in a column. Because you would have to perform an update everyday to update anyone with a birthday today. It is much better to store only the Birthdate and calculate all and any other values needs when you query that record.
Andrew SQLDBA
October 25, 2013 at 3:51 am
declare @BDate datetime = '01 jan 1990'
select DATEDIFF(yy,@BDate, getdate()) as AgeInYears, DATEDIFF(mm,@BDate, getdate()) as AgeInMonths, DATEDIFF(WW,@BDate, getdate()) as AgeInWeeks
October 25, 2013 at 9:31 am
manickavasagam.g (10/25/2013)
declare @BDate datetime = '01 jan 1990'select DATEDIFF(yy,@BDate, getdate()) as AgeInYears, DATEDIFF(mm,@BDate, getdate()) as AgeInMonths, DATEDIFF(WW,@BDate, getdate()) as AgeInWeeks
Take a look at what happens when you change @BDate to '01 nov 1990' 😉
Try this:
selectDATEDIFF(yy,@BDate, getdate())-
case when getdate()<dateadd(yy,datediff(yy,@BDate,getdate()), @BDate) then 1 else 0 end as AgeInYears
-- Itzik Ben-Gan 2001
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply