Calculate Age in SQL Server Express

Hi,

In SQL Server Express 2017, I want to calculate Age in a table containing 100 records.
Let us say different DOBs are saved some 25 years before today. In the new column Age, this result is desired 25y 5m 3d.

-San
Sanjay GandhiFounder, KenhalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulEE Topic AdvisorCommented:
0
NorieAnalyst Assistant Commented:
Try this.

DATEDIFF(YY,DOBField, GetDate()) -
       CASE WHEN DATEADD(YY,DATEDIFF(YY,DOBField, GetDate()),DOBField)> GetDate() THEN 1
            ELSE 0
       END
AS [Age]
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sanjay GandhiFounder, KenhalAuthor Commented:
Both answers can be tweaked to arrive at a combined result.
However, it requires a little long code to find the answer.

This is a result in years and months.
Update statement is applied to update AccountAge checking AccountOpenDate compared with current date.

Update Customers
Set AccountAge = Str(DateDiff(YEAR,AccountOpenDate,GetDate())-IIF(Month(AccountOpenDate) <= Month(GetDate()),0,1),2,0) + 'y ' + Str(IIF(Month(AccountOpenDate) <= Month(GetDate()), Month(GetDate())-Month(AccountOpenDate), 12-Month(AccountOpenDate)+Month(GetDate())),2,0) + 'm'

Thanks.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

NorieAnalyst Assistant Commented:
I assumed you wanted the age in years only.

By the way, why are you storing the age in a table?
0
Sanjay GandhiFounder, KenhalAuthor Commented:
On a monthly basis, the Account age is to be calculated, so that based on age of the account, the processes can be carried out. For example, mapping some plans and sharing them with the Customers based on the age of the their account. So it will be easy to work on saved age of the Account.
0
NorieAnalyst Assistant Commented:
If that's the case you shouldn't be updating the age every month in the table, you should use a query that returns the age and use that.
0
PortletPaulEE Topic AdvisorCommented:
Why not create a view that contains the age calculation?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.