May 26, 2010 at 8:36 pm
I have a table called member which contains memberid and DOB. How can I calculate age to date based on Date of Birth.
Thanks,
May 26, 2010 at 8:53 pm
You can use DATEDIFF function:
LIke:
SET DATEFORMAT DMY
SELECT DATEDIFF(yy,'21-03-1985',GETDATE()) AGE
May 26, 2010 at 9:35 pm
ColdCoffee (5/26/2010)
You can use DATEDIFF function:LIke:
SET DATEFORMAT DMY
SELECT DATEDIFF(yy,'21-03-1985',GETDATE()) AGE
Since DateDiff returns the number of boundaries crossed in comparing these two dates, it will report the wrong age if today's date is not >= your DOB. So, use this instead:
SELECT CASE WHEN datepart(dayofyear, GetDate() ) >= datepart(dayofyear, @DOB)
THEN DATEDIFF(yy,@DOB,GETDATE())
ELSE DATEDIFF(yy,@DOB,GETDATE()) -1
END AGE
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 26, 2010 at 9:57 pm
WayneS (5/26/2010)
ColdCoffee (5/26/2010)
You can use DATEDIFF function:LIke:
SET DATEFORMAT DMY
SELECT DATEDIFF(yy,'21-03-1985',GETDATE()) AGE
Since DateDiff returns the number of boundaries crossed in comparing these two dates, it will report the wrong age if today's date is not >= your DOB. So, use this instead:
SELECT CASE WHEN datepart(dayofyear, GetDate() ) >= datepart(dayofyear, @DOB)
THEN DATEDIFF(yy,@DOB,GETDATE())
ELSE DATEDIFF(yy,@DOB,GETDATE()) -1
END AGE
That perfectly makes sense!
How sweet it is to start the day with learning a titbit.. wow
Thanks Wayne Shef! 🙂
May 28, 2010 at 9:00 am
Another way to calculate the age is:
SELECT (ABS(CAST(CONVERT(char(8),GETDATE(),112) AS int)
- CAST(CONVERT(char(8),@DOB,112) AS int)) / 10000) [AGE];
Here is a link to where I originally saw this type of calculation. It is in the comments section. :
http://www.sqlmag.com/article/tsql3/datetime-calculations-part-5.aspx
May 28, 2010 at 9:21 am
May 28, 2010 at 9:26 am
I'd second Lynn's article. It's a good discussion of this topic.
September 25, 2012 at 12:09 pm
Wayne,
This doesn't work on leap years.
Try these dates:
@DOB = '09/26/2011'
GetDate() = '09/25/2012'
mitch
September 25, 2012 at 1:02 pm
You can use the function on the link below to find the age:
Age Function F_AGE_IN_YEARS
September 25, 2012 at 1:39 pm
Adam Gojdas (5/28/2010)
Another way to calculate the age is:
SELECT (ABS(CAST(CONVERT(char(8),GETDATE(),112) AS int)
- CAST(CONVERT(char(8),@DOB,112) AS int)) / 10000) [AGE];
Here is a link to where I originally saw this type of calculation. It is in the comments section. :
http://www.sqlmag.com/article/tsql3/datetime-calculations-part-5.aspx
Careful now... Because of the date conversions to the CHAR datatype, that will be relatively slow.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 25, 2012 at 1:43 pm
Michael Valentine Jones (9/25/2012)
You can use the function on the link below to find the age:Age Function F_AGE_IN_YEARS
Any chance of making it NOT a scalar function?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 25, 2012 at 2:42 pm
Jeff Moden (9/25/2012)
Careful now... Because of the date conversions to the CHAR datatype, that will be relatively slow.
It is beautiful though - this has long been a favourite of mine (I think it was Rob Farley that first showed it to me).
As far as performance is concerned, well yes it will be slow. But then, all T-SQL solutions will be slow, right? 😉
Just kidding (mostly) - it's a good point that conversion to string is particularly bad. And especially scalar T-SQL UDFs :sick:
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
September 25, 2012 at 3:18 pm
Jeff Moden (9/25/2012)
Michael Valentine Jones (9/25/2012)
You can use the function on the link below to find the age:Age Function F_AGE_IN_YEARS
Any chance of making it NOT a scalar function?
I originally wrote this for SQL 2000, so be my guest. 🙂 The code could be greatly simplified too, probably enough to convert it to inline code.
I posted it mainly to give them an example of some code that I know I tested fairly completely.
February 17, 2013 at 6:59 pm
Jeff Moden (9/25/2012)
Michael Valentine Jones (9/25/2012)
You can use the function on the link below to find the age:Age Function F_AGE_IN_YEARS
Any chance of making it NOT a scalar function?
SELECT DATEDIFF(yy, 0, GETDATE() - DOB) Age_Way1,
YEAR(GETDATE()-DOB) -1900 Age_Way2
_____________
Code for TallyGenerator
February 19, 2013 at 3:44 pm
My two bits...
declare @dob datetime
set @dob = '2/29/2000'
select convert(char(10),@dob,101) as DOB,
case
when ((MONTH(@dob) * 100) + DAY(@dob)) > ((MONTH(getdate()) * 100) + DAY(getdate()))
then DATEDIFF(year,@dob,getdate()) - 1
else DATEDIFF(year,@dob,getdate()) End as AgeInYears
Viewing 15 posts - 1 through 15 (of 23 total)
You must be logged in to reply to this topic. Login to reply