How to determine persons age

  • Given birthdate in datetime format and a given date in the future, how do I determine the persons age? Remember leap years, etc.

  • 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]

  • Thanks. I'll try those examples.

  • 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

  • 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