0

I have looked online and through my book, with no luck on finding the command. It is the final part of the problem. "Display the current ages of all the employees." My table and columns are.

Table: Employee
Columns: Names(VARCHAR(20),null), 
         Address(VARCHAR(20),null), 
         Employee_Number(INT,null), 
         Salary(SMALLMONEY,null), 
         Birthdate(DATE,null), 
         Employment_Date(DATE,null).

Everything I have found online is to convert one or two birth dates into age. Is there a command to convert multiple ones with out typing in the birth date into the query?

DECLARE @dob datetime
SET @dob= '1992-01-09 00:00:00"

Would I have to do

SELECT NAMES
DECLARE @dob date
SET @dob= 'yyyy-mm-dd'

Something like this, I think there is 37 lines of names that I would need to convert there birth date into age.

Any help is much appreciated, thanks in advance for help.

Leo
  • 6,480
  • 4
  • 37
  • 52
siz
  • 21
  • 1
  • 2
  • 1
    possible duplicate of http://stackoverflow.com/questions/1572110/how-to-calculate-age-in-years-based-on-date-of-birth-and-getdate – Sankara Feb 17 '14 at 23:17
  • check http://www.katieandemil.com/sql-age-function-calculate-accurate-age-using-sql-server – Leo Feb 17 '14 at 23:29

1 Answers1

1

You already have have the birthdate column stored as date. To calculate the age, you just need the difference between it and now (which can be returned by the built in function getdate():

SELECT name, DATEDIFF(year, GETDATE(), birthdate) AS age
FROM   employee
Mureinik
  • 297,002
  • 52
  • 306
  • 350