Important alert: (current site time 7/15/2013 7:14:00 PM EDT)
 

VB icon

Calculate Age

Email
Submitted on: 7/25/2000 8:35:24 PM
By: Umachandar  
Level: Intermediate
User Rating: By 2 Users
Compatibility: SQL Server 7.0, SQL Server 6.5 and earlier
Views: 35109
 
     How to calculate AGE in years. (note: The DATEDIFF function in MS SQL Server gives the distance between year boundaries & as such cannot be used to calculate the age.)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Calculate Age
-- Description:How to calculate AGE in years. (note: The DATEDIFF function in MS SQL Server gives the distance between year boundaries & as such cannot be used to calculate the age.)
-- By: Umachandar
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=151&lngWId=5--for details.--**************************************

declare @birthday datetime, @d datetime
select @birthday = '12/31/1998', @d = '1/1/99'
select datediff(yy, @birthday, @d) -
	(case when (datepart(m, @birthday) > datepart(m, @d)) or
			(datepart(m, @birthday) = datepart(m, @d) And
				datepart(d, @birthday) > datepart(d, @d))
			then 1
			else 0
	end) as Age1
-- Just for completion, another way to do the same
select year ( dateadd( dd, datediff ( dd , @birthday , @d ) ,
		'1900-01-01 00:00' ) )	- 1900
-- To find out if a year is leap year or not, use
if ( year ( @d ) % 400 = 0 ) Or
	( year( @d ) % 4 = 0 And year ( @d ) % 100 <> 0 )
	print 'Leap Year - 366 days'
else
	print 'Not a leap year - 365.25 days'


Other 133 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
10/17/2000 5:08:04 PMPaul

I thought a year had 365 days on a regular year. Isn't the .25 for the average including the leap year???
(If this comment was disrespectful, please report it.)

 
8/31/2001 10:05:54 AMKevin Meagher

Great stuff -- datediff is a pain in the neck sometimes (while extremely useful). I'm using this to calcualate time till next annual event (even if the 1st time the event happened was 20 years ago). Much thanks for the example.
(If this comment was disrespectful, please report it.)

 
9/11/2001 8:10:12 AMNaresh Gupta

Great Stuff
One Question - In the second alternative why to use dateadd when this can work
select year ((datediff(dd , '09-13-1975' , getdate()))) - 1900
If any special reason pls do mention.

Thanks
(If this comment was disrespectful, please report it.)

 
5/31/2002 4:45:01 PMJacob Kornit

I have done this in a function a little differently.
Search for 'Function' and 'Juvenile'

(If this comment was disrespectful, please report it.)

 
11/29/2008 9:32:18 AM

well done.. tnx i'll try..
(If this comment was disrespectful, please report it.)

 
8/7/2009 7:21:42 AMpankaj

u have created an account in planet-source-code and pass is 9250336115
(If this comment was disrespectful, please report it.)

 
1/8/2010 8:40:29 AMAlejandro Carrillo

I have made a function exactly.
Search "age exactly" or "edad exacta"
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.