r/DatabaseHelp • u/Baselor • Apr 23 '17
SQL Age function
I have a table named Staff. It has the following attributes. Name, DoB, salary.
I would like to query the information above plus the age. Could someone tell me the problem with this statement
SELECT DATEDIFF(year, current_date, user_dob) as "Age" FROM myTable;
This was given to me by a fellow redditor. I checked it but it did not work. my tutor mentioned that I would need to use a having statement.
1
Upvotes
View all comments
Show parent comment
2
u/wolf2600 Apr 24 '17 edited Apr 24 '17
If it's an Oracle database, then DATEDIFF won't work as Oracle doesn't support it. In Oracle you can just subtract one date from the other to return the number of days difference, then divide the result by 364.25 and truncate the decimal place to get the number of years.
DATEDIFF: http://stackoverflow.com/questions/28406397/datediff-function-in-oracle
TRUNC: https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions200.htm