Home Forums SQL Server 7,2000 T-SQL How can we calculate Age of employee? RE: How can we calculate Age of employee?

  • Lynn Pettis (1/24/2013)


    r_slot (1/24/2013)


    I am using your code of course.

    Good. Now, if you would please post the code you are are using, not just refer me to my old code, we can see what may be happening.

    It looks fine to me. Here's your code Lynn, tweaked only to read table values - the "errant" ones posted earlier:

    SELECT

    [day],

    dob,

    age = datediff(yy,dob,[day]) -

    case when [day] < dateadd(yy,datediff(yy,dob,[day]), dob) then 1 else 0 end

    FROM (

    SELECT [day] = '1992-07-07', dob = '1927-04-24' UNION ALL -- 65

    SELECT [day] = '2000-06-06', dob = '1915-01-31' UNION ALL -- 85

    SELECT [day] = '2003-12-15', dob = '1923-06-22' UNION ALL -- 80

    SELECT [day] = '2002-03-22', dob = '1917-01-24' UNION ALL -- 85

    SELECT [day] = '2008-02-28', dob = '2007-03-01' -- 0

    ) d

    Results

    [font="Courier New"]

    day________dob________age

    1992-07-071927-04-2465

    2000-06-061915-01-3185

    2003-12-151923-06-2280

    2002-03-221917-01-2485

    2008-02-282007-03-010

    [/font]


    [font="Arial"]Low-hanging fruit picker and defender of the moggies[/font]

    For better assistance in answering your questions, please read this[/url].


    Understanding and using APPLY, (I)[/url] and (II)[/url] Paul White[/url]

    Hidden RBAR: Triangular Joins[/url] / The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url] Jeff Moden[/url]