I have a person table which contains DateOfBirth column and and a Age column
how it's possible to drive age from date of birth, I want to fill age automatically
regards,
How to fill a column using other columns
Page 1 of 12 Replies - 625 Views - Last Post: 25 May 2012 - 12:57 PM
Replies To: How to fill a column using other columns
#2
Re: How to fill a column using other columns
Posted 25 May 2012 - 12:58 AM
There isn't really any point in that. You'll have to use triggers to set it initially and scheduled events to keep them up to date. It's a whole lot of trouble that, in the end, is completely unnecessary.
By having a DateOfBirth column in your table, you already have what you need to calculate the Age of the person. Having a separate Age column duplicates data you are already have. - You can calculate the Age easily enough based on the DateOfBirth in your queries, rather than returning an Age field. For example, this is a simple way:
If you don't feel like doing that in all your queries, you could always set up a view that "acts" like it has an Age field.
Then you can do:
By the way, those examples are using MySQL functions. You didn't specify which database you are using, so I just chose one at random.
By having a DateOfBirth column in your table, you already have what you need to calculate the Age of the person. Having a separate Age column duplicates data you are already have. - You can calculate the Age easily enough based on the DateOfBirth in your queries, rather than returning an Age field. For example, this is a simple way:
SELECT
FLOOR(DATEDIFF(NOW(), DateOfBirth) / 365) AS Age
FROM Person
If you don't feel like doing that in all your queries, you could always set up a view that "acts" like it has an Age field.
CREATE VIEW PersonWithAge AS
SELECT
DateOfBirth,
FLOOR(DATEDIFF(NOW(), DateOfBirth) / 365) AS Age
FROM Person;
Then you can do:
SELECT DateOfBirth, Age FROM PersonWithAge;
By the way, those examples are using MySQL functions. You didn't specify which database you are using, so I just chose one at random.
This post has been edited by Atli: 25 May 2012 - 01:01 AM
#3
Re: How to fill a column using other columns
Posted 25 May 2012 - 12:57 PM
Thank you
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|