459,317 Members | 1,339 Online
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

# Calculating Age median from list of Dates of Birth

 P: 2 Hii friends I need to calculate the age median from the given dates of birth in an employee database in MS SQl. Please help... Roma Feb 12 '08 #1
5 Replies

 Expert 100+ P: 1,134 do a datediff between the current date and their birth date then take the average for example in months it is Expand|Select|Wrap|Line Numbers SELECT avg(datediff(m,getdate(),birthdate)) FROM employees   is that what you mean Feb 12 '08 #2

 Expert 100+ P: 227 Hii friends I need to calculate the age median from the given dates of birth in an employee database in MS SQl. Please help... Roma Hi, for calculating the age use the following sample query and change it according to your requirement DECLARE @FromDate DATETIME, @ToDate DATETIME SELECT @FromDate = '1983-07-04',@ToDate = '2008-07-04' SELECT CASE WHEN @FromDate > @ToDate THEN NULL WHEN DATEPART(day, @FromDate) > DATEPART(day, @ToDate) THEN DATEDIFF(month, @FromDate, @ToDate) - 1 ELSE DATEDIFF(month, @FromDate, @ToDate) END / 12.0 thanks Feb 12 '08 #3

 Expert 100+ P: 227 do a datediff between the current date and their birth date then take the average for example in months it is Expand|Select|Wrap|Line Numbers SELECT avg(datediff(m,getdate(),birthdate)) FROM employees   is that what you mean Hi, if the birth date is 2007-01-31 and today is 2008-02-01 and in the code SELECT avg(datediff(m,getdate(),birthdate)) FROM employees the function datediff(m,getdate(),birthdate) will give you the difference as 1 month though it is differ by 1 day. Feb 12 '08 #4

 Expert 100+ P: 1,134 Hi, if the birth date is 2007-01-31 and today is 2008-02-01 and in the code SELECT avg(datediff(m,getdate(),birthdate)) FROM employees the function datediff(m,getdate(),birthdate) will give you the difference as 1 month though it is differ by 1 day. Then use days avg(datediff(d,getdate(),birthdate)) Feb 12 '08 #5

 P: 2 Hey Thanks everyone for the code.... Feb 13 '08 #6