Avatar of goldmineexpert
goldmineexpert
 asked on

adding age to SQL query column

i would like to calculate the age in a column next to the date column

1954    58
Microsoft SQL Server

Avatar of undefined
Last Comment
Anthony Perkins

8/22/2022 - Mon
ChetOS82

SELECT YEAR(datecolumn), FLOOR(DATEDIFF(day,datecolumn,GETDATE())/365.242199)
Ashok

never mind
Patrick Matthews

The expression in http:#a38139048 may be unreliable.  For example, suppose you have someone born on 2010-06-30, and you measured the age on 2011-06-30.

Most everyone would agree that the answer should be 1 year old, but the expression above will yield 0.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Patrick Matthews

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
keyu

SELECT (year(gatedate())-year column) as age

or else if iyou have date column than

SELECT (year(gatedate())-year(datecolumn)) as age
Patrick Matthews

keyu,

You do realize that that approach can give you a result of one year old when the difference in dates can be as little as one day, right?

Patrick
keyu

yah sorry my mistake..go for this..


SELECT (year(gatedate())-year_column) as age

or else if iyou have date column than

SELECT (year,datecolumn,gatedate()) as age
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Anthony Perkins

keyu,

Please check your solution before posting.  Patrick has already pointed out once why your solution cannot be used.  Now you have posted it again and it does not even compile, let alone get the right result.
Swindle

I would definitely not like to see this solution returned by someone searching for a similar problem.  Both mine and Matthewspatrick's solutions were correct and he even attempted to explain why the accepted solution was not a good one.  I would vote for one of the truly correct answers to get marked as the accepted solution or the question deleted entirely so it doesn't confuse anyone later.
Anthony Perkins

I would vote for one of the truly correct answers to get marked as the accepted solution or the question deleted entirely so it doesn't confuse anyone later.
I agree.  Points should be awarded here http:#a38139144 and here http:a#38140055
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
keyu

bymistake datedif removed ... but questioner got the idea so according to me just minor correction is required here..for future questioner...

if that idea works for the applicant i dont find anything wrong with it..

SELECT datediff(year,datecolumn,gatedate()) as age

anyways...you can proceed further what ever you feel right about this..
Anthony Perkins

so according to me just minor correction is required here..for future questioner...
No.  It clearly illustrates a fundamental error in your perception of how DATEDIFF actually works.  It also, shows that you are unprepared to even test the code you have posted not once, but three times or you would know that at the very least it does not even compile.