﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Age / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 23:38:57 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>My two bits...[quote]declare @dob datetimeset @dob = '2/29/2000'select convert(char(10),@dob,101) as DOB,	case 	when ((MONTH(@dob) * 100) + DAY(@dob)) &amp;gt;  ((MONTH(getdate()) * 100) + DAY(getdate()))		then DATEDIFF(year,@dob,getdate()) - 1	else DATEDIFF(year,@dob,getdate()) End as AgeInYears[/quote]</description><pubDate>Tue, 19 Feb 2013 15:44:01 GMT</pubDate><dc:creator>jshahan</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>[quote][b]Jeff Moden (9/25/2012)[/b][hr][quote][b]Michael Valentine Jones (9/25/2012)[/b][hr]You can use the function on the link below to find the age:Age Function F_AGE_IN_YEARS[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74462[/url][/quote]Any chance of making it NOT a scalar function?[/quote][code="sql"]SELECT DATEDIFF(yy, 0, GETDATE() - DOB) Age_Way1,            YEAR(GETDATE()-DOB) -1900 Age_Way2 [/code]</description><pubDate>Sun, 17 Feb 2013 18:59:12 GMT</pubDate><dc:creator>Sergiy</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>[quote][b]Jeff Moden (9/25/2012)[/b][hr][quote][b]Michael Valentine Jones (9/25/2012)[/b][hr]You can use the function on the link below to find the age:Age Function F_AGE_IN_YEARS[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74462[/url][/quote]Any chance of making it NOT a scalar function?[/quote]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.</description><pubDate>Tue, 25 Sep 2012 15:18:28 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>[quote][b]Jeff Moden (9/25/2012)[/b][hr]Careful now... Because of the date conversions to the CHAR datatype, that will be relatively slow.[/quote]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 [i]T-SQL[/i] 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:</description><pubDate>Tue, 25 Sep 2012 14:42:36 GMT</pubDate><dc:creator>Paul White</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>[quote][b]Michael Valentine Jones (9/25/2012)[/b][hr]You can use the function on the link below to find the age:Age Function F_AGE_IN_YEARS[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74462[/url][/quote]Any chance of making it NOT a scalar function?</description><pubDate>Tue, 25 Sep 2012 13:43:16 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>[quote][b]Adam Gojdas (5/28/2010)[/b][hr]Another way to calculate the age is:[code="sql"]SELECT (ABS(CAST(CONVERT(char(8),GETDATE(),112) AS int)      - CAST(CONVERT(char(8),@DOB,112) AS int)) / 10000) [AGE];[/code]Here is a link to where I originally saw this type of calculation.  It is in the comments section. :[url]http://www.sqlmag.com/article/tsql3/datetime-calculations-part-5.aspx[/url][/quote]Careful now... Because of the date conversions to the CHAR datatype, that will be relatively slow.</description><pubDate>Tue, 25 Sep 2012 13:39:49 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>You can use the function on the link below to find the age:Age Function F_AGE_IN_YEARS[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74462[/url]</description><pubDate>Tue, 25 Sep 2012 13:02:02 GMT</pubDate><dc:creator>Michael Valentine Jones</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>Wayne,This doesn't work on leap years.Try these dates:@DOB = '09/26/2011'GetDate() = '09/25/2012'mitch</description><pubDate>Tue, 25 Sep 2012 12:09:41 GMT</pubDate><dc:creator>mitch 14804</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>I'd second Lynn's article. It's a good discussion of this topic.</description><pubDate>Fri, 28 May 2010 09:26:14 GMT</pubDate><dc:creator>Steve Jones - SSC Editor</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>You can also check out this [url=http://www.sqlservercentral.com/articles/T-SQL/63351/][b]article[/b][/url] and the discussion that followed.</description><pubDate>Fri, 28 May 2010 09:21:32 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>Another way to calculate the age is:[code="sql"]SELECT (ABS(CAST(CONVERT(char(8),GETDATE(),112) AS int)      - CAST(CONVERT(char(8),@DOB,112) AS int)) / 10000) [AGE];[/code]Here is a link to where I originally saw this type of calculation.  It is in the comments section. :[url]http://www.sqlmag.com/article/tsql3/datetime-calculations-part-5.aspx[/url]</description><pubDate>Fri, 28 May 2010 09:00:19 GMT</pubDate><dc:creator>Adam Gojdas</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>[quote][b]WayneS (5/26/2010)[/b][hr][quote][b]ColdCoffee (5/26/2010)[/b][hr]You can use DATEDIFF function:LIke:[code="sql"]SET DATEFORMAT DMYSELECT DATEDIFF(yy,'21-03-1985',GETDATE()) AGE[/code][/quote]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 &amp;gt;= your DOB. So, use this instead:[code="sql"]SELECT CASE WHEN datepart(dayofyear, GetDate() ) &amp;gt;= datepart(dayofyear, @DOB)            THEN DATEDIFF(yy,@DOB,GETDATE())             ELSE DATEDIFF(yy,@DOB,GETDATE()) -1        END AGE[/code][/quote]That perfectly makes sense! How sweet it is to start the day with learning a titbit.. wowThanks Wayne Shef! :-)</description><pubDate>Wed, 26 May 2010 21:57:33 GMT</pubDate><dc:creator>ColdCoffee</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>[quote][b]ColdCoffee (5/26/2010)[/b][hr]You can use DATEDIFF function:LIke:[code="sql"]SET DATEFORMAT DMYSELECT DATEDIFF(yy,'21-03-1985',GETDATE()) AGE[/code][/quote]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 &amp;gt;= your DOB. So, use this instead:[code="sql"]SELECT CASE WHEN datepart(dayofyear, GetDate() ) &amp;gt;= datepart(dayofyear, @DOB)            THEN DATEDIFF(yy,@DOB,GETDATE())             ELSE DATEDIFF(yy,@DOB,GETDATE()) -1        END AGE[/code]</description><pubDate>Wed, 26 May 2010 21:35:58 GMT</pubDate><dc:creator>WayneS</dc:creator></item><item><title>RE: Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>You can use DATEDIFF function:LIke:[code="sql"]SET DATEFORMAT DMYSELECT DATEDIFF(yy,'21-03-1985',GETDATE()) AGE[/code]</description><pubDate>Wed, 26 May 2010 20:53:16 GMT</pubDate><dc:creator>ColdCoffee</dc:creator></item><item><title>Age</title><link>http://www.sqlservercentral.com/Forums/Topic928705-392-1.aspx</link><description>I have a table called member which contains memberid and DOB.  How can I calculate age to date based on Date of Birth.Thanks,</description><pubDate>Wed, 26 May 2010 20:36:44 GMT</pubDate><dc:creator>QQ-485619</dc:creator></item></channel></rss>