Datediff(yyyy,getdate(),bi
That will give num years diff aka Age.
Main Topics
Browse All TopicsHi all. I am trying to get all records back from a database and I am passing in some parameters to get some records back. The parameters I am passing in are the ages of people that I want to return...a min age and max age, zipcode, miles and gender...basic people searching.
In the code below, see how I am selecting birthdate?...I don't want to do that....I want to convert birthdate to age and then match the min age and match age accordingly...meaning...I want the search to return all records that are between certain ages, but I don't want to store the age in the db because its always changing...I want it to be calculated from Birthdate...can somebody please help???
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Above comment was assuming Birthdate as Datetime column.
If not convert it into Datetime and then do Datediff..
And note that this would tell the completed years and not the running years..
If you need it to be compared for running years, then Add 1 to make it work like
and DATEDIFF(YY, Birthdate, getdate()) + 1 between @AgeMin and @AgeMax
Do you need the ages as of a certain date or just the current date?
How accurate do you need this age calculation? (Day, Month, Year)
Is this SQL running in a stored proc?
==============
Note: I find the DateDiff() function to be a bit too simplistic to use for such calculations. It simply subtracts the M or D or Y portion of two dates. It isn't really giving you an AGE difference so much as it is giving you a year difference. While the DateDiff() function would be accurate on the person's birthday, it would miss calculate the person's AGE on every day leading up to that person's birthday.
AGE calculation is a 'step function'. You are a certain age, in years, until your birthday (birthdate anniversary), when you suddenly become one year older.
=================
Since I don't see this approach posted...you can convert the @AgeMin and @AgeMax to dates and compare them to the birthdate. This would be much more efficient in a stored proc.
Here are 2 formulas I use to get the exact "age":
Not taking leap years into consideration (fine for most scenarios):
select cast(datediff(d, Birthdate, getdate()) AS DECIMAL (8,1))/365
Taking leap years into consideration (more precise):
select cast(datediff(d, Birthdate, getdate()) AS DECIMAL (8,1))/365.25
aikimark: I am reading your post, and am trying that one out, and so far it seems to work...but, so I am learning...what is the difference between what you posted and say, what victoria posted???
btw, I appreciate you putting all the code I would need for the where clause....there was a missing parentheses, but I figure it out no biggie.
thanks, to everyone...so so much...i am not quite done yet, but will let you know about points asap.
yes, thank you rrjegan17, by running years, you mean like the date you are and then adding all extra days ...like, I turn 36 tomorrow, but I am not yet 36 today, but on my birthday tomorrow, I will be 36...right?
so that would be in the where clause as well??? I will try that also right now. Thanks so much.
right victoria: my age min and max are numbers...that is correct....because they are just being passed in from an aspx form...like
I want this data where the people I am looking for are between ages 18 (dropdownlist) and 32(dropdown list), but in the database, when they create their profile, I don't want them to have to enter in their age. The database stores the birthdate in a datetime field. So, that is why I want it to convert to age and compare the age to the birthdate...so I guess the birthdate would have to be converted to age for comparison, right?
Bolenka,
Exactly. I actually came up with this code originally when I had to show the age of buildings acquired on a report and all I had was the acquired date in the database. I didn't want to see the results as something like 39 years and 4 months and 2 days. I wanted to see the decimal equivalent, or 39.3388. I think this works perfectly for your example, as well.
@Bolenka
You either convert the birthdate into an age (at a specific date) or you convert the (integer) min and max age to dates. If this is a big table, a stored proc can initialize two date variables from the two age parameters, comparing each row's birthdate value without any more data conversions. This should perform much faster than converting every row's birthdate value into an age.
Business Accounts
Answer for Membership
by: rrjegan17Posted on 2009-09-09 at 19:32:11ID: 25297041
Instead of:
and Age >= @AgeMin and Age<=@AgeMax
Replace with
and DATEDIFF(YY, Birthdate, getdate()) between @AgeMin and @AgeMax