July 24, 2003 at 8:42 am
Given birthdate in datetime format and a given date in the future, how do I determine the persons age? Remember leap years, etc.
July 24, 2003 at 8:48 am
Hi rgetsy,
quote:
Given birthdate in datetime format and a given date in the future, how do I determine the persons age? Remember leap years, etc.
what about this thread?
http://www.sqlservercentral.com/forum/link.asp?TOPIC_ID=14304
Cheers,
Frank
Sorry,
actually I meant this one
http://www.sqlservercentral.com/forum/link.asp?TOPIC_ID=14085
Edited by - a5xo3z1 on 07/24/2003 08:52:13 AM
--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/[/url]
July 24, 2003 at 9:45 am
Thanks. I'll try those examples.
July 24, 2003 at 10:38 am
Whoops, I think I replied to one of those threads in a link.
Out of curiousity, why do you need to be concerned with leap years when determining a person's age? Were you thinking about dividing days into years or something? This is how I'd do it:
declare @bd datetime
select @bd = '1976-12-2'
select case when month(@bd) > month(getdate()) then datediff(year, @bd, getdate())-1
when month(@bd) = month(getdate()) and day(@bd) > day(getdate()) then datediff(year, @bd, getdate())-1
when month(@bd) = month(getdate()) and day(@bd) <= day(getdate()) then datediff(year, @bd, getdate())
when month(@bd) < month(getdate()) then datediff(year, @bd, getdate())
end
Oh. I did it with getdate()) lemme fix this up:
declare @bd datetime
declare @fd datetime
select @bd = '1976-2-2'
select @fd = '2004-2-1'
select case when month(@bd) > month(@fd) then datediff(year, @bd, @fd)-1
when month(@bd) = month(@fd) and day(@bd) > day(@fd) then datediff(year, @bd, @fd)-1
when month(@bd) = month(@fd) and day(@bd) <= day(@fd) then datediff(year, @bd, @fd)
when month(@bd) < month(@fd) then datediff(year, @bd, @fd)
end
Edited by - spongemagnet on 07/24/2003 10:47:26 AM
-Ken
July 25, 2003 at 12:27 am
sometimes less code is more readable, use the else-structure.
declare @bd datetime
declare @fd datetime
select @bd = '1976-2-2'
select @fd = '2004-2-1'
select case
when month(@bd) > month(@fd)
then datediff(year, @bd, @fd)-1
when month(@bd) = month(@fd) and day(@bd) > day(@fd)
then datediff(year, @bd, @fd)-1
else datediff(year, @bd, @fd)
end
Johan
Learn to play, play to learn !
Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere :w00t:
- How to post Performance Problems
- How to post data/code to get the best help[/url]
- How to prevent a sore throat after hours of presenting ppt
press F1 for solution, press shift+F1 for urgent solution 😀
Need a bit of Powershell? How about this
Who am I ? Sometimes this is me but most of the time this is me
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply