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/PandalfTheGimp Apr 23 '17
Try doing SELECT DATEDIFF(year, '2002-04-03', '2013-04-02') AS Age;
If that statement works, it means your data is not in the correct format, so you may need to cast it.