SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can calculate age

    hi
    I need help to sql


    How can calculate (age) from brith.

    I have tablesql)

    create table stu(id number(2),name varchar2(8),brith date,age number(2));

    =======================================
    id ==== name ==== brith ==== age
    =======================================
    01 === smith === 06/07/1980 === ????????
    02 === beater === 22/10/1975 === ?????????



    thanks

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    See this post
    (ain't that forum search function great )

  3. #3
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I tried this link and it dos not work;

  4. #4
    SitePoint Wizard
    Join Date
    Jul 2003
    Location
    Kent
    Posts
    1,921
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    yes it does...

  5. #5
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see.
    sorry
    I mean the code dos not work not link

    I want column "age" calculate automatic.

    I enter only date in column "birth" and
    column "age" calculate automatic age.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Exactly, that's what rudy's code does. Of course, you'll have to change some table and field names
    Code:
    select 
        id
      , name
      , birth
      , year(current_date) 
           - year(birth)
           - case when month(current_date)
                     > month(birth)      
                  then 0
                  when month(current_date)
                     < month(birth)      
                  then 1
                  when dayofmonth(current_date)
                     < dayofmonth(birth) 
                  then 1
                  else 0 end    as age
      from stu
    Btw, once you save the date of birth, it doesn't make much sense storing the age in your table as well, since you'd have to recalculate it everytime again anyway. Just calculate it when you select the data from the table.

  7. #7
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this my problem
    in attachments
    Attached Images Attached Images

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by saad1 View Post
    this my problem
    your problem seems to be that you are trying to run a mysql query in an oracle database system

    you posted this question in the mysql forum and got a mysql solution

    what database system are you really interested in using?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SitePoint Member
    Join Date
    Jan 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    realy I have not noted this forum for mysql
    I think sql for database

    I am very sorry

  10. #10
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,220
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    not a problem

    which database system are you using? oracle?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


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
  •