| Author |
Topic |
|
Bill_C
Constraint Violating Yak Guru
299 Posts |
Posted - 2010-02-24 : 04:35:42
|
| How do I calculate someones age between a given DOB and GETDATE() |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-02-24 : 04:38:14
|
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=64762see the section on Finding Age KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-02-24 : 04:40:26
|
| SELECT DatePart(Year, GETDATE()) - DatePart(Year, DOB) - 1Vabhav T |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-02-24 : 04:43:08
|
quote: Originally posted by vaibhavktiwari83 SELECT DatePart(Year, GETDATE()) - DatePart(Year, DOB) - 1Vabhav T
Why the "-1" ?If the DOB is current year you will get -1 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Bill_C
Constraint Violating Yak Guru
299 Posts |
Posted - 2010-02-24 : 04:44:23
|
| I thought that just gives the years difference between the two dates, what if the difference is 8yrs 11 months? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-02-24 : 04:46:50
|
quote: Originally posted by vaibhavktiwari83 SELECT DatePart(Year, GETDATE()) - DatePart(Year, DOB) - 1Vabhav T
Dont directly rely on DatediffSELECT datediff(year,'2009-12-31 23:59:59.997','2010-01-01 00:00:00.00')MadhivananFailing to plan is Planning to fail |
 |
|
|
Bill_C
Constraint Violating Yak Guru
299 Posts |
Posted - 2010-02-24 : 04:47:27
|
| as in select datediff(yyyy,'01 Mar 1957',getdate())= 53when in fact the age should be 52 + 11 months! |
 |
|
|
Bill_C
Constraint Violating Yak Guru
299 Posts |
Posted - 2010-02-24 : 04:48:26
|
| oops, cross post, I'm looking for exact age, thanks |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-02-24 : 04:49:39
|
find the difference in month then convert to years / monthsSELECT datediff(month, DOB, getdate()) / 12, datediff(month, DOB, getdate()) % 12 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-02-24 : 04:50:44
|
| Its a big topic of discussion i should not post this query directly.because to show year months and days there will be different queryVabhav T |
 |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2010-02-24 : 04:53:22
|
quote: Originally posted by Bill_C I thought that just gives the years difference between the two dates, what if the difference is 8yrs 11 months?
SELECT datediff(month,dob,getdate())%12,datediff(month,dob,getdate())/12 from table_nameSenthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
 |
|
|
Bill_C
Constraint Violating Yak Guru
299 Posts |
Posted - 2010-02-24 : 05:12:59
|
| Thanks all. |
 |
|
|
|