SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    uk
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    age from datetime

    i have a database and need to extract people who are either 16, 17 or 18 yrs old. i have DOB as a datetime field in the db.

    whats the syntax to add to my where clause to just pull back this age range?

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends of the database you use. Some have function for that, when you must use a bit logic in other.
    Which DB engine are you using?

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    approximate age can be obtained by extracting the year from both the birth date and the current date

    however, exact age is a more complex calculation

    if your birthday is 1978-12-21 then as of today you are still 29
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,323
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by tripy View Post
    Which DB engine are you using?
    my guess is SQL Server

    nobody else would store DOB in a datetime column

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    uk
    Posts
    128
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes sql server...

    its ok, cos its a one off for now, ive simply used a query to created a copy of the original db and pulled in dob then manually deleted the irrelevant records outside the age range.


Bookmarks

Posting Permissions

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