Calculate Age in SQL Server Express


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.

Sanjay GandhiFounder, KenhalAsked:
Who is Participating?
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:
NorieAnalyst Assistant Commented:
Try this.

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

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'

Ensure Business Longevity with As-A-Service

Using the as-a-service approach for your business model allows you to grow your revenue stream with new practice areas, without forcing you to part ways with existing clients just because they don’t fit the mold of your new service offerings.

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

By the way, why are you storing the age in a table?
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.
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.
PortletPaulEE Topic AdvisorCommented:
Why not create a view that contains the age calculation?
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

From novice to tech pro — start learning today.