add computed column from birthdate and time of registration column

I have a birtdate column in date format in my table named tblJJDP.  I need to create a computed column (AgeReg) that automatically calculates the birthdate and the Date of Registration (DOR) column.

So if the person was born on 1/1/00 and they were registered on 1/1/12, then the AgeReg column should show 12 years old.

Column names are

DOR - date type
Birthdate  -date type
AgeReg - integer type
al4629740Asked:
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.

Anthony PerkinsCommented:
It depends how accurate you want to be.  :)

Scott has covered this more than once so I will not repeat here, but here is one of his solutions:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_28090389.html

If that is indeed what you want to use then it is trivial to convert that to a computed column.
0
Scott PletcherSenior DBACommented:
To copy my own answer for here :-).

Code below handles everything ... but a Feb 29th birthday is treated as Mar 1st, not Feb 28, otherwise you need special code just for Feb 29th :-(.


DATEDIFF(YEAR, BirthDate, DOR) -
        CASE WHEN CONVERT(char(5), BirthDate, 101) > CONVERT(char(5), DOR, 101) THEN 1 ELSE 0 END
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
al4629740Author Commented:
How do I output the results to the AgeReg column?
0
Anthony PerkinsCommented:
ALTER TABLE tblJJDP ADD AgeReg AS DATEDIFF(YEAR, BirthDate, DOR) -
        CASE WHEN CONVERT(char(5), BirthDate, 101) > CONVERT(char(5), DOR, 101) THEN 1 ELSE 0 END
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
Microsoft SQL Server

From novice to tech pro — start learning today.