Query to calculate the age

Hello

Please suggest me to wirte a function that can calculate the age.

Hello

No need for a function. But a simple calculation.

SQL> select round(months_between(sysdate,to_date('11-MAY-1977','dd-MON-YY'))/12) age from dual;

       AGE
----------
        33

If you take into account the dates stored in an oracle table column can take care of the years
before and after 1950.
A thread for your reference: Re: day airthmetic

Tags: Database

Similar Questions

  • Calculate the age of a person from today

    I'm writing a query where I'm trying to figure out how old a person is starting today.

    So, I have a database field in my STUDENT_TB (table) where the birth_date is a DATE field. I'm writing a query where I would like to calculate the age of the person.

    So, if a person is 48 years old and 6 months 2 days, I would essentially say the person is 48 years old. Any help is greatly appreciated.

    I tried using MONTHS_BETWEEN but run into some problems. I tried using P_END_DATE - P_START_DATE and then he dive by 365, but which is at the origin of the problems too.

    Thank you!

    Roxyrollers wrote:
    I'm writing a query where I'm trying to figure out how old a person is starting today.

    So, I have a database field in my STUDENT_TB (table) where the birth_date is a DATE field. I'm writing a query where I would like to calculate the age of the person.

    So, if a person is 48 years old and 6 months 2 days, I would essentially say the person is 48 years old. Any help is greatly appreciated.

    I tried using MONTHS_BETWEEN but run into some problems. I tried using P_END_DATE - P_START_DATE and then he dive by 365, but which is at the origin of the problems too.

    Thank you!

    What exactly are the problems you are having?

    select
       floor(months_between(sysdate, to_date('01-jan-1978','dd-mon-yyyy')) / 12) as years_old
    from dual;
    
     YEARS_OLD
    ----------
         32
    
    1 row selected.
    
    TUBBY_TUBBZ?
    
  • Calculate the (age) years between two dates

    Nice day!

    I have not been able to find an answer, that I was able to use in the archives or online and would be very grateful for the help.  I'm working with Adobe Acrobat Pro XI (not LiveCycle) and I have very little experience with programming (for the most part, copy / paste).

    I need to create a text box that calculates the number of years between two dates entered, i.e. the age when the date of birth and death are given.  Also, would be nice if she was a Virgin until the two were provided.  Anyone would be able to give me the code I need to put in the custom calculation script?

    Once again, any help would be much appreciated!  Thank you and have a wonderful day!

    For the fields for Date of birth DOB and DOD for the Date of death and age in complete years both by using a'd-mmm-yyyy' date format you can use a custom calculation to the JavaScript for the field of age as:

    function GetField (cName) {}
    get the field with error trapping object;
    oField var = this.getField (cName);
    test error;
    if(oField == null) app.alert ("error during the" + cName, 1, 0);
    Returns the field object;
    return oField;
    } / / end GetField function;

    function {Scand (cFormat, cString)
    using date string format convert object date;
    convert string to date using format;
    Kai var = util.scand (cFormat, cString);
    the value null is an error of conversion;
    if(oDate == null) app.alert ("Conversion error" cString "by using the format" + cFormat, 1, 0);
    date of return;
    Return to oDate;
    } / / end Scand function;

    clear the value of the field;

    Event.Value ="";
    get data dob and dod;
    var oDOB = GetField ("DOB");
    Var o = GetField ("DOD");

    treat only if DOB and DOD have value and are not null;
    If (oDOB! = null & o! = null & oDOB.value! = "" & oDOD.value!) = "") {}

    convert the DOB and DOD to date objects.
    oDOB var = util.scand ("d-mmm-yyyy", oDOB.value);
    O var = util.scand ("d-mmm-yyyy", oDOD.value);

    get the difference over the years;
    var Age = oDOD.getFullYear () - oDOB.getFullYear ();

    adjust for month of previous DOD DOB;
    If (oDOD.GetMonth)< odob.getmonth())="">

    adjust the DOD before DOB date in the month of birth;

    If (oDOD.GetMonth () == oDOB.getMonth () & oDOD.getDate ())< odob.getdate())="">
    Set the value of the field;

    Event.Value = Age;
    }

  • query to calculate the value and produce new lines

    QUARTER CUSTOMER PRODUCT RETAIL_SALES_AMT WHOLESALE_AMT

    01/01/2006 ABC VACUUM CLEANER 454234,00 65633456.00
    01/04/2006 ABC VACUUM CLEANER 324562,00 45333234.00
    01/07/2006 ABC VACUUM CLEANER 67845423.00 NULL
    01/10/2006 ABC VACUUM CLEANER 67453453.00 NULL
    01/01/2007 ABC VACUUM CLEANER 56754633.00 NULL
    01/04/2007 ABC VACUUM CLEANER 45423434.00 NULL


    Hi guys,.

    It's a situation where I have to produce a few new lines with projections based on Q4 RETAIL_SALES_AMT
    RETAIL_SALES_AMOUNT and fourth ' rs following WHOLESALE_AMT. As you can see from the sample data for a specific customer,
    product I have populated only until 01/04/2006 retail_sales_amt but WHOLE_SALE amt for the same product and customer
    are there up to 01/04/2007.

    I have to produce a PROJECTED RETAIL_SALES_AMT and it must be inserted in a new line with an indicator to identify
    a proposed line. Here, in this case I have to produce a new line of projection from 07/01/2006,10/01/2006 and 01/01/2007
    the RETAIL_SALES_AMT. The method of calculation is provided for in:

    retail_sales_amt scheduled for 07/01/2006=.345+ ((01/07/2006 whole_sales-01/04/2006 whole_sales) / (01/04/2006 whole_sale)))
    * 01/04/2006 RETAIL_SALES_AMT and move forward to subsequent quarters.

    Is it possible that I can use a query to produce these new lines by calculating the RETAIL_SALES_AMT on the fly, or any other
    How to procedure.

    Please help as it seems a little complicated.

    Concerning

    Published by: user626688 on October 27, 2009 11:26

    Published by: user626688 on October 27, 2009 11:26

    Published by: user626688 on October 27, 2009 11:27

    Published by: user626688 on October 27, 2009 11:28

    Published by: user626688 on October 27, 2009 11:31

    Published by: user626688 on October 27, 2009 11:32

    Hello

    As far as I can tell, that's what you asked for:

    WITH     got_prev         AS
    (
         SELECT     table_x.*
         ,     LAG (wholesale_amt) OVER ( PARTITION BY  customer
                                        ,                product
                                ORDER BY      quarter
                               )     AS prev_wholesale_amt
         FROM     table_x
    --     WHERE     ...     -- Any filtering goes here
    )
    ,     tree     AS
    (
         SELECT     got_prev.*
         ,     SYS_CONNECT_BY_PATH ( CASE
                               WHEN  LEVEL = 1
                               THEN  retail_sales_amt
                               ELSE  ( .345
                                              + wholesale_amt
                                              - prev_wholesale_amt
                                  ) / prev_wholesale_amt
                              END
                            , '*'
                            )                    AS path
         FROM     got_prev
         START WITH     retail_sales_amt     IS NOT NULL
         CONNECT BY     retail_sales_amt     IS NULL
              AND     quarter               = ADD_MONTHS (PRIOR quarter, 3)
              AND     customer          = PRIOR customer
              AND     product               = PRIOR product
    )
    SELECT       quarter
    ,       customer
    ,       product
    ,       COALESCE ( retail_sales_amt
                 , eval_number ( LTRIM ( path
                                           , '*'
                               )       )
                 )      AS retail_sales_amt
    ,       wholesale_amt
    ,       NVL2 ( retail_sales_amt
                , 'F'
                , 'T'
                )          AS projected_flag
    FROM       tree
    ORDER BY  customer
    ,            product
    ,       quarter
    ;
    

    I posted earlier the eval_number function.

    In this query, a group of consecutive quarters, where the first group a retail_sales_num and the rest of the members of the group are not, is treated as a hierarchy. The retail_sales_amt of all members (except the first) will be based on the previous, as well as the wholesale_amts past and present.
    Say that a tree is 5 points of time (as in your examples of data). We can calculate the 2nd point in several ways: using analytical functions, for example. But we cannot use the same formula to calculate the 3rd point, because the calculation of section 2 must be completed before we can calculate the 3rd. It goes the same for the 4th and 5th.
    This is CONNECT BY arrives. CONNECT BY is one thing in Oracle SQL that can be recursively; children may find themselves once their parents are found, in the same way we want to calculate the nth retail_sales_amt once the amount of the n-minus-1 has been calculated. To do this, we use SYS_CONNECT_BY_PATH, where the first element in the path is the retail_sales_amt given, and all others are the factor to multiply this number to get the next amount.
    SYS_CONNECT_BY_PATH produces a string like ' * 324562 *. 4965935 *-. 0057739', which should be interpreted as a number. TO_NUMBER won't: TO_NUMBER cannot convert only a single numeric literal. Instead, we have a function defined by the user who put this string dynamically in the SELECT clause of a query, where it is interpreted as a numeric expression.

  • How to calculate the age?

    Hi again!

    I would like to calculate an age registered users using the date of birth entered by the user. I want to show that age on the profile of the user.

    Does anyone know an easy way to do this?

    If dateOfBirth variable with their date of birth then is as follows: -.

  • Calculate the age (months and years) of two dates

    Trying to get the (almost exact) age in years and months to fill in 'Age at the time of retirement' (years1) since the date of birth (DOB1) and the date of their departure in retirement (retireDate1). The only examples that I could find are dates "today." If anyone can help with code? And I understand it works better with JavaScript but FormCalc would also work?

    Dates as DD/MM/YYYY

    Field is num {z9}

    Thank you in advance for your help!

    I could do this work with Age1 as a text field. Of course, you can do whatever you want with it, but this method makes the most sense for me because I can say 99 months "99 years". If you need to separate the year and month in different areas, you should be able to do it easily. Finally, it requires that the user does not manually enter dates. (Something on the raw values isn't quite right when they came from the selection.)

    Age1::calculate (or anywhere where you want this event)

    if (!DOB1.isNull && !retireDate1.isNull){
      var d1 = DOB1.rawValue.split('-');
      var d2 = retireDate1.rawValue.split('-');
      var years = parseInt(d2[0]) - parseInt(d1[0]);
      var months = (parseInt(d2[1]) - parseInt(d1[1]));
      if (parseInt(months) < 0){
        months = 12 + months;
        years--;
      }
      Age1.rawValue = years + " years " + months + " months.";
    }
    
  • How can I use a date in the format YYYYMMDD "" to calculate an age?

    I do a form and I want to use a military format "YYYYMMDD" date to calculate the age of the user. I found the code to find the age based on a date in the format "yyyy/mm/dd", but I have trouble finding the age after adding slashes with javascript after the date input. I use Adobe Acrobat Pro DC on OSX 10.11.6. Thanks in advance.

    Replace these two lines:

    var cDob is getField (cBirthDate) .value;.

    var cCutoff is getField (cCutoffDate) .value;.

    With this:

    var cDob is getField (cBirthDate) .valueAsString;.

    var cCutoff is getField (cCutoffDate) .valueAsString;.

  • Query for the age calculation problem

    Hello

    I am trying to calculate the age using MONTHS_BETWEEN, then divide by 12

    CREATE TABLE DOB (date of DATE_OF_BIRTH);

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('13-JAN-54','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('26-APR-54','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('30-JUN-29','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('03-JAN-67','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('04-AUG-49','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('27-DEC-47','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('21-MAR-67','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('26-SEP-56','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('06-MAY-52','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('01-JAN-00','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('07-JUL-63','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('24-JAN-62','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('22-OCT-63','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('15-OCT-36','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('06-SEP-36','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('27-FEB-60','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('03-NOV-75','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('08-APR-52','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('21-APR-60','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('22-MAR-57','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('21-SEP-63','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('27-JAN-73','DD-MON-RR'));

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('29-SEP-65','DD-MON-RR'));

    Select trunc (MONTHS_BETWEEN (SYSDATE, DOB. (DATE_OF_BIRTH) / 12) AS the "âge" of DOB.

    Output

    Age

    ---

    59

    59

    -15

    46

    -35

    -34

    46

    56

    61

    13

    50

    51

    49

    -23

    -22

    53

    37

    61

    53

    56

    49

    40

    47

    I get some as negative age and others as positive tried using "MON-DD-YY" but same result.

    any suggestion

    Thanks for your help.

    Hello

    The formula you use is a good way to get the age in years.
    The problem is that some of your "date of birth" are in the future.  For example:

    Insert into values of birth date (DATE_OF_BIRTH) (to_date('30-JUN-29','DD-MON-RR'));

    We hear June 30, 2029, a little less than 16 years into the future.  When you truncate this number, you get-15...

    In RR version, the actual year is between 2000 and 2049, years-00 to 49 years old are considered as in the current century, but 2000 to 2049.

    This is an example of why you should always use figures 4 years and "YYYY" format, like this:

    Insert into (DATE_OF_BIRTH) date of birth values (to_date (June 30, 1929 ', "DD-MON-YYYY"));

  • Query to find the sum of the different groups in the same column

    Hello

    I have a table as follows:



    visitors
    ------------------------------------------------------------------------------------------
    months of amt credit CustID
    --------------------------------------------------------------------------------------------
    001 C 2000-Jan-2012

    001D 5000 Feb 2012

    001C 3000 Mar-2012

    001C 3000-Apr-2012

    001D 7000-may-2012



    I have to write a single query to calculate the sum of the credit and the sum of the flow values separately.


    Thank you and best regards,
    SB2011

    Hello

    Select decode (credit,' it, sum (amt), sum (amt)) in the Group of customers to credit;

    Published by: 937440 on 24 February 2013 07:06

  • Between the prompt on the age

    Gurus and Experts,

    I invited on the age of the column between edit box.
    I calculate the age as BOX WHEN "BOB Datamart. » ». "" "" BOBDBO '. "" "" "Dim_INVOLVED_PARTY_DIM_Client '." " BIRTH_DT' = TIMESTAMP ' 01/01/01 00:00:00 ' AND THEN ANOTHER NULL (TIMESTAMPDIFF (SQL_TSI_DAY, TIMESTAMP ' 1900-01-01 12:00 ', VALUEOF (NQ_SESSION.)) (("" CURRENT_DT ")) * 86400000 - TIMESTAMPDIFF (SQL_TSI_DAY, TIMESTAMP ' 1900-01-01 12:00", "BOB Datamart" "." ") ». "" "" BOBDBO '. "" "" "Dim_INVOLVED_PARTY_DIM_Client '." " (BIRTH_DT") * 86400000) / (365.25 * 86400000) END
    The age is displayed up to 2 decimal places. The problem is that if a user is prompting the same values in the ex 30.25 30.25 editing area, the results not obtaining appears when there with age 30.25.

    THX.

    then you can try with this formula right?

    --

     TmestampAdd(SQL_TSI_YEAR, DateOfBirth, CURRENT_DATE) 
    

    --

    Sorry... should be: timestampDiff

     TmestampDiff(SQL_TSI_YEAR, DateOfBirth, CURRENT_DATE) 
    

    Published by: Kishore Guggilla, October 19, 2010 18:19

  • The age factor

    Guyz,

    I have a text field on my module called DOB (Date of birth) and a disply Age column. How to calculate and display the age in my area of age.

    have me/JJ/AAAA format of data when the user enters the data in DOB on key-next-item trigger, it will calculate the age and treats the next field.

    How can I calculate this age?

    Concerning

    Houda

    something like this:

    select trunc( ( sysdate - :DOB ) / 365 ) from dual
    

    or

    :AGE :=  trunc( ( sysdate - :DOB ) / 365 ) ;
    
  • Age of query optimization in the year

    Hey there,

    I calculate the difference of the year between two dates by doing the following thing:
    TRUNC(MONTHS_BETWEEN(RES.SAMPLE_SAMPLING_DATE,RES.PATIENT_BIRTHDATE)/12)+1
    Do you think I can optimized the query or you know other ways to do the same calculation?

    Kind regards
    Jarod.

    Have you already searched this forum?
    There are many examples...

  • I have problems with D4.1 in Labview ' 09. It is said to determine the age of a person in years, when given the month in course, day, and year and month of birth of the person, day, and year

    Seen someone's birth month day and year... and also the day of the current month and year like entries... calculate their age, years... as output... age should not increment by one until the arrival of their birthday this year.   I can't get the coding right... Please help


  • calculate the scalar product

    Hi all

    hope that everyone else is doing great! I have a question about a problem that was asked to do. It says I have to calculate the scalar product, so I wonder if I just did? The constraints are that one way is to make ite with only discreet from the digital palette functions and the other functions of the range of linear algebra matrix. So I just hope that it is the right way to do it.

    Thank you

    Because in the dark ages, we had no linear algebra nice and convenient functions, you should know how to do it "manually". This is the point of most of the homework problems. To make you understand the concept then you know what it really means and the implementation to let you know how do it yourself.

    Just like in the dark ages we didn't have Google, so we used these buildings called libraries. And you looked up things in these large big cabinets full of drawers that had hundreds of cards in them. And you had to know the Dewey decimal system.

  • Using two dates to find the age...

    Hello

    I need to find the age of a person the DOB entered into service.  the age may be days or years depending on when the person was born.

    If the person was born is less than a year, so I should be able to calculate the number of days on the other, it should be over the years.

    I can't find the right way to do it.  I tried like this but its does not work as it should:

    todaysDate long = (new Date()) .getTime ();
    long date of birth = dobDateField.getDate ();

    long dateDiff = todaysDate - date of birth;

    multiply a day with 365 days (ignore the leap year for now)
    length of year = DateTimeUtilities.ONEDAY * 365;

    If (dateDiff< year)="">
    int NoOfDays = (int) (dateDiff / DateTimeUtilities.ONEDAY);
    ageEditField.setText (String.valueOf (NoOfDays) + "D");
     
    } else {}
    int NoOfYears = (int) (dateDiff/year);
    ageEditField.setText (String.valueOf (NoOfYears));
    }

    Help, please...

    int age = (diff / (1000 * 60 * 60 * 24));
    int years = 0;
    
    if( age >= 365 ) {
       years = age / 365;
    }
    
    if( years > 0 ) {
     // use years
    } else {
     // use days
    }
    

    Something like that.

Maybe you are looking for