I don't know if this is a stupid question but I've been asked to create a contact list and there's a column requirement for the contacts dob DATE & age INT. My question is how would I be able to get the value for 'age' to adjust as time goes by based on their 'dob'? I've never done anything like this before using SQL statements.
if the table contains only DOB, could you not define a view which calculates age on the fly?
That was specifically my question. I looked online and searched here to see if anyone has ever "defined" a view which calculates age on the fly in the 'age' column based on the 'dob' column but I couldn't find any examples of how to formulate this in SQL. I'm assuming based on your response, that it can be. My question is how?
Originally Posted by r937
]if that doesn't satisfy whoever it is that "asked" you to have an age column, ask them to look up PHB in wikipedia
I checked PHB in Wikipedia and unless we're referencing a Dilbert character or college degree, I have no idea what you're referencing here.
Sorry - it wasn't clear to me that you suggest I omit the 'age' column in place if the AGE() function. It may seem elementary to you guys but I've only been using PG since 10/10 and am slowly learning everything by reading and self implementation. Thanks for the suggestion!
Why not just calculate it dynamically during your select?
date_part('years', age(current_date, dob)) as age,
I guess I don't quite follow this code based on my table parameters:
ide=# \d users
Column | Type | Modifiers
id | integer | not null
fname | character varying(40) | not null
lname | character varying(40) | not null
email | character varying | not null
office | integer | not null
dob | date | not null
"users_pkey" PRIMARY KEY, btree (id)
ide=# SELECT * FROM users;
id | fname | lname | email | office | dob
1 | Carlos | Mennens | firstname.lastname@example.org | 124 | 1980-05-12
When you suggest the code above, I'm having a hard time understanding the relation to my table.
I tried to relate the example but I'm missing something:
ide=# SELECT dob('years',age(current_date, dob)) as age WHERE id = 1;
ERROR: column "dob" does not exist
LINE 1: SELECT dob('years', age(current_date, dob)) as age WHERE id =...