2 Replies - 742 Views - Last Post: 25 May 2012 - 12:57 PM Rate Topic: -----

#1 hamidkhl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

How to fill a column using other columns

Posted 24 May 2012 - 11:32 PM

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,
Is This A Good Question/Topic? 0
  • +

Replies To: How to fill a column using other columns

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

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:
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

Was This Post Helpful? 1
  • +
  • -

#3 hamidkhl  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 118
  • Joined: 03-November 08

Re: How to fill a column using other columns

Posted 25 May 2012 - 12:57 PM

Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1