Search Postgresql Archives

Re: Auto Adjust Age

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Apr 6, 2011 at 11:15 PM, Jerry Sievers <gsievers19@xxxxxxxxxxx> wrote:
> Carlos Mennens <carlos.mennens@xxxxxxxxx> writes:
>
>> CREATE TABLE users
>> (
>>    id integer PRIMARY KEY UNIQUE NOT NULL, --ID
>>    fname character varying(40) NOT NULL, --First name
>>    lname character varying(40) NOT NULL, --Last name
>>    email character varying NOT NULL, --email address
>>    office integer NOT NULL, --Office number
>>    dob date NOT NULL, --Date of birth
>>    age integer NOT NULL --Age
>> )
>> ;
>>
>> Is there a way in SQL I can have the users 'age' be auto adjusted
>> based on the 'id' & 'dob'? I would assume this is possible because if
>> you have 100 employees, I doubt someone has time to sit and change
>> everyone's age from 31 > 32 on their birthday. Can someone please help
>> explain how this works or what the SQL code would look like assuming
>> that it's possible? I have no advanced far enough to see what triggers
>> and views are so perhaps it's just my level with SQL in general.
>
> I'd suggest not storing age but instead wrapping with a view that calls
> date_trunc('year', now() - dob).
>
> If you must store the age then you have to run a daily batch to make the
> bulk update.

Theres also the age() function....

SELECT age(dob);

Should give you

           age
-------------------------
 31 years 5 mons 17 days

If you want to be really exact about it. :)

JC de Villa

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux