Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2009
    Posts
    93

    Unanswered: Calculate Age Range

    I have a date of birth datefield (dob) and found the following query to extract ages of people etc. in my table. How can further this query to bring back ranges (e.g. find only the ages of people between 30 and 40)

    SELECT DOB, GETDATE() AS RightNow, CASE WHEN DATEPART(month, DOB) < DATEPART(month, GETDATE()) THEN DATEDIFF(year, DOB, GETDATE())
    WHEN DATEPART(month, DOB) > DATEPART(month, GETDATE()) THEN DATEDIFF(year, DOB, GETDATE()) - 1 WHEN DATEPART(day, DOB)
    <= DATEPART(day, GETDATE()) THEN DATEDIFF(year, DOB, GETDATE()) ELSE DATEDIFF(year, DOB, GETDATE()) - 1 END AS Age
    FROM mytable

  2. #2
    Join Date
    Oct 2009
    Posts
    93
    Thanks to Pootle, i picked it up here http://www.dbforums.com/microsoft-sq...range-age.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •