Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 compute the age

Author  Topic 

cwtriguns2002
Constraint Violating Yak Guru

272 Posts

Posted - 2007-03-05 : 04:19:30
Hi all. How could i get the age of the employees given the birthdate.

table - personalinfo

name birthdate
john 2/15/2004 12:00:00 AM
peter 2/15/2003 12:00:00 AM
jon 2/15/2001 12:00:00 AM
mike 2/15/2000 12:00:00 AM
sam 2/15/2002 12:00:00 AM

Thanks
-Ron-

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-05 : 04:23:23
see
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64762&SearchTerms=Finding%20Age


KH

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-05 : 04:23:54
also
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=78730


KH

Go to Top of Page

cwtriguns2002
Constraint Violating Yak Guru

272 Posts

Posted - 2007-03-05 : 04:44:58
The queries are very complicated...i don't understand them. Im just a beginner in sql. :( Is it possible to make a simple query that calculates the year now minus year of birth?

Thanks
-Ron-
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-05 : 04:51:24
1. Go to http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74462
2. Copy the function F_AGE_IN_YEARS and paste into your Query Analyzer and run
3. Then run the following

select [Age] = dbo.F_AGE_IN_YEARS( birthdate, getdate() )
from personalinfo
-- using F_AGE_IN_YEARS from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=74462



KH

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-05 : 05:01:40
or if you are not after accuracy, just use datediff(year)

select [age] = datediff(year, birthdate, getdate() )
from personalinfo



KH

Go to Top of Page

cwtriguns2002
Constraint Violating Yak Guru

272 Posts

Posted - 2007-03-05 : 05:13:45
Uhhhhmmmm. Fantastic. Thanks KH.
by the way, im confused of F_AGE_IN_YEARS... is it a function? After running it, where is it saved? do i need to copy it in my report designer?

-Ron-
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-05 : 05:16:49
F_AGE_IN_YEARS is a function. After running it, it will created and exists in your database. Unless you drop the function, it will forever there.


KH

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-05 : 05:25:19
SELECT DATEDIFF(YEAR, '20061231 23:59:59', '20070101 00:00:00')

equals to 1 year difference...


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -