Link to home
Start Free TrialLog in
Avatar of al4629740
al4629740Flag for United States of America

asked on

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
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

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:
https://www.experts-exchange.com/questions/28090389/What-query-would-I-run-in-SQL-Server-2008-R2-to-calculate-the-age-of-each-subject.html

If that is indeed what you want to use then it is trivial to convert that to a computed column.
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of al4629740

ASKER

How do I output the results to the AgeReg column?
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