#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Need help for start sql


    hi,
    i am new in sql and i need your help,
    i m trying to find the age from year of brith and peoles who have more then 25 years old
    so i have a tabel

    T1
    id.p name year
    ----------------------------------
    1 jack 1975
    2 rocky 1999
    3 robaun 1945
    4 cyral 1992

    so i try

    SELECT year FROM T1 WHERE YEAR > 25


    but this isn't work, how can i do?
    thanks for help
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    Paris Uppland
    Posts
    2,383
    Rep Power
    391
    Code:
    select <columnsOfInterest>
      from t1
     where year(getdate()) - year > 25
  4. #3
  5. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,212
    Rep Power
    4279
    alternatively...
    Code:
    WHERE t1.year < YEAR(GETDATE()) - 25
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    117
    Rep Power
    9
    DATEDIFF(YEAR, '1/1/' + cast(ryear as CHAR(4)), GETDATE()) > 25
    will work too
  8. #5
  9. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,212
    Rep Power
    4279
    but which of these approaches will allow the query to be optimized to use an index, and which will force a table scan?

    there's more to writing a query than just getting the right answer
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    117
    Rep Power
    9
    Actually, execution plan will be the same in all 3 variants...

IMN logo majestic logo threadwatch logo seochat tools logo