I would like to implement a query to add an AGE column to my table that would calculate the age of each record using the date column (mcsDate) that exists already.
Could anyone help me with this?
Thanks!
I would like to implement a query to add an AGE column to my table that would calculate the age of each record using the date column (mcsDate) that exists already.
Could anyone help me with this?
Thanks!
Create Computed Column
CREATE TABLE [dbo].[DateDemo]
(
[MDate] [date] NULL,
[age] AS (datediff(day, [MDATE], getdate())) -- This is a computed column
);
If you want to calculate age in years the way humans do birthdates, you should calculate it the way humans calculate ages. It's really not difficult. It's the only way to ensure that you have no errors from leap years.
CREATE TABLE DateTable (
BirthDate DATE,
Age AS YEAR(getdate()) - YEAR(BirthDate) - CASE
WHEN MONTH(BirthDate) > MONTH(getdate())
OR (
MONTH(BirthDate) = MONTH(getdate())
AND DAY(BirthDate) > DAY(getdate())
)
THEN 1
ELSE 0
END
);
Be advised if you're programming for international programs, however, that some countries use East Asian age reckoning.