How to find the difference in date in years, months and days

Hello
I need to find the difference between 2 dates in the following way "years months days". "
Please can help me, how can I achieve this.

for example, in the scott schema emp table, I need to find the difference in date between sysdate and hiredate for an employee in the following way.

12 years 7 months 4 days.

Hello

Please, see this post to AskTom [difference between 2 dates | http://asktom.oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:96012348060]. There is good information in this forum, for example you can also see this thread [calculation of years, months & days | http://forums.oracle.com/forums/thread.jspa?messageID=3115216�]

Kind regards

Published by: Walter Fernández on November 30, 2008 08:58 - adding another link

Tags: Database

Similar Questions

  • How to get the difference in Date in years, months and days

    Select Sysdate-Birth_date from Employees;
    the query returns the number of days between two dates
    I want to convert this number in years, months and days
    Can someone help me please

    Hoek wrote:
    Try:

    SQL> with t as (
    2  select to_date('01-01-1980', 'dd-mm-yyyy') birthdate
    3  ,      to_date('05-05-2010', 'dd-mm-yyyy') today
    4  from   dual
    5  )
    6  --
    7  --
    8  --
    9  select extract(year from today)-extract(year from birthdate) years
    10  ,      extract(month from today)-extract(month from birthdate) months
    11  ,      extract(day from today)-extract(day from birthdate) days
    12  from   t;
    
    YEARS     MONTHS       DAYS
    ---------- ---------- ----------
    30          4          4
    
    1 row selected.
    

    (May need some adjustments, possibly using the LARGEST, but currently not as long)

    Indeed, a few adjustments...

    SQL> ed
    Wrote file afiedt.buf
    
      1  with t as (
      2    select to_date('31-01-2009', 'dd-mm-yyyy') birthdate
      3    ,      to_date('28-02-2010', 'dd-mm-yyyy') today
      4    from   dual
      5    )
      6  --
      7  --
      8  --
      9  select extract(year from today)-extract(year from birthdate) years
     10  ,      extract(month from today)-extract(month from birthdate) months
     11  ,      extract(day from today)-extract(day from birthdate) days
     12* from   t
    SQL> /
    
         YEARS     MONTHS       DAYS
    ---------- ---------- ----------
             1          1         -3
    
    SQL>
    

    Maybe something like this?

    SQL> ed
    Wrote file afiedt.buf
    
      1  with t as (select to_date('17-nov-2006','dd-mon-yyyy') as c_start_date, to_date('21-jan-2008','dd-mon-yyyy') as c_end_date from dual union all
      2             select to_date('21-nov-2006','dd-mon-yyyy'), to_date('17-feb-2008','dd-mon-yyyy') from dual union all
      3             select to_date('31-jan-2009','dd-mon-yyyy'), to_date('28-feb-2010','dd-mon-yyyy') from dual union all
      4             select to_date('21-jun-2006','dd-mon-yyyy'), to_date('17-jul-2008','dd-mon-yyyy') from dual
      5             )
      6  -- end of test data
      7  select c_start_date, c_end_date
      8        ,trunc(months_between(c_end_date, c_start_date) / 12) as yrs
      9        ,trunc(mod(months_between(c_end_date, c_start_date), 12)) as mnths
     10        ,trunc(c_end_date - add_months(c_start_date, trunc(months_between(c_end_date, c_start_date)))) as dys
     11* from t
    SQL> /
    
    C_START_DATE        C_END_DATE                 YRS      MNTHS        DYS
    ------------------- ------------------- ---------- ---------- ----------
    17/11/2006 00:00:00 21/01/2008 00:00:00          1          2          4
    21/11/2006 00:00:00 17/02/2008 00:00:00          1          2         27
    31/01/2009 00:00:00 28/02/2010 00:00:00          1          1          0
    21/06/2006 00:00:00 17/07/2008 00:00:00          2          0         26
    
  • How to find the difference between standard edition and standard edition one

    How to find the difference between oracle database standard edition and standard edition one 64 bit

    (a) using sql
    (b) using the configuration/installation files

    How to find the difference between oracle database standard edition and standard edition one 64 bit

    (a) using sql

    Select * the option of $ v;

    (b) using the configuration/installation files

    opatch lsinventory-details

  • How to find the last update date, time and user of the file field peoplecode

    How to find the last updated date time fields of peoplecode records?

    Thank you.

    We can check the update date-time using the following query

    SELECT LASTUPDDTTM IN THE PSPCMPROG WHERE OBJECTVALUE1 LIKE "RECNAME" AND OBJECTVALUE2 AS "FIELDNAME".

  • How to find the number of data items in a file written with the ArryToFile function?

    I wrote a table of number in 2 groups of columns in a file using LabWindows/CVI ArrayToFile... Now, if I want to read the file with the FileToArray function so how do I know the number of items in the file. during the time of writing, I know how many elements array to write. But assume that I want the file to be read at a later time, then how to find the number of items in the file, so that I can read the exact number and present it. Thank you all

    Hello

    I start with the second question:

    bytes_read = ReadLine (file_handle, line_buffer, maximum_bytes);

    the second argument is the buffer to store the characters read, so it's an array of characters; It must be large enough to hold maximum_bytes the value NULL, if char [maximum_butes + 1]

    So, obviously the number of lines in your text tiles can be determined in a loop:

    Open the file

    lines = 0;

    While (ReadLine () > 0)

    {

    lines ++;

    }

    Close the file

  • How to convert days to years, months and days remaining

    Hi all
    I have the number of days for example: 398 days how
    convert 398 days to several years, and the number of months and days remaining
    398 days 1 year, 1 month and 2 days
    Concerning
    Jocelyne

    There is no correct answer.

    That's right! ;)

  • How to find the difference between two dates in time except Sunday

    Hi all

    I have a table, as shown below.
    SQL> select * from test;
    
    TR_ID                                              CREATE_TIME                                                                       CODE
    -------------------------------------------------- --------------------------------------------------------------------------- ----------
    S12341                                             05-JUN-12 12.20.52.403000 AM                                                      1003
    S12342                                             11-JUN-12 11.15.33.182000 AM                                                      1003
    S12342                                             07-JUN-12 12.00.36.573000 PM                                                      1002
    S12343                                             20-JUN-12 12.34.37.102000 AM                                                      1003
    S12343                                             15-JUN-12 11.34.27.141000 PM                                                      1002
    S12344                                             01-JUL-12 10.01.06.657000 PM                                                      1002
    S12344                                             06-JUL-12 12.01.04.188000 AM                                                      1003
    S12341                                             31-MAY-12 11.20.38.529000 PM                                                      1002
    I would like to know the difference between same tr_ids create_time, which should give out in hours except Sunday.

    For example:

    TR_ID: S12344
    1002_Create_time: July 1, 12 PM 10.01.06.657000 (i.e. Sunday)
    1003_Create_time: 12.01.04.188000 AM 6 July 12

    1002 create time is 22:00 Sunday.

    If the query must exclude only the hours of Sunday which is 10 p.m. to Monday 00 h which is 2 Hrs.

    I tried the sub query after doing a search on this forum but I am not getting the desired output.
    SELECT count(*) FROM (SELECT ROWNUM RNUM,tr_id,create_time CT_1002 FROM test c WHERE c.tr_id='S12344' and 
    ROWNUM <= (select (cast(a.create_time as date)-cast((select create_time from test b where a.tr_id=b.tr_id and code=1002) as date)) 
    from test a where a.code=1003 and a.tr_id=c.tr_id) + 1) d 
    WHERE to_char(cast((select create_time from test e where e.tr_id=d.tr_id and code=1002) as date) + RNUM - 1, 'DY') NOT IN('SUN');
    Need help to get the desired o/p

    Hello

    If I unederstand the problem correctly, that's what you want:

    WITH       got_extrema     AS
    (
         SELECT       tr_id
         ,       CAST (MIN (create_time) AS DATE)     AS start_date
         ,       CAST (MAX (create_time) AS DATE)     AS end_date
         FROM       test
         GROUP BY  tr_id
    )
    SELECT       tr_id
    ,       start_date, end_date          -- If wanted
    ,       24 * ( ( ( TRUNC (end_date,   'IW')          -- Count -1 day for every full week
                        - TRUNC (start_date, 'IW')
                 )
               / -7
                  )
                + LEAST ( end_date               -- If end_date is a Sunday
                            , TRUNC (end_date, 'IW') + 6     -- consider it 00:00:00 on Sunday
                     )
                - CASE
                          WHEN  start_date >= TRUNC (start_date, 'IW') + 6     -- If start_date is a Sunday
                   THEN  TRUNC (start_date, 'IW') + 6               -- consider it 00:00:00 Sunday
                   ELSE  start_date
                      END
                )     AS total_hours
    FROM      got_extrema
    ;
    

    I guess that you don't need to worry about fractions of a second. As you did in the code you have posted, I am to convert the TIMESTAMP to date values, because of DATE arithmetic and functions are so much better than what is available for timestamps.

    Basically, it's just to find the number of days between start_date and end_date and multiplying by 24, with these twists:
    (a) 1 day is deducted for each week between start_date and end_date
    (b) if End_date is a Sunday, none of the end_date himself hours are counted
    (c) If start_date is a Sunday, then all the start_date himself hours are counted. Why these hours should be counted? Because 1 day is already being deducted for the week which includes start_date, which contains only this Sunday.

    TRUNC (dt, 'IW') is the beginning of the ISO week containing dt; in other words, 00:00:00 the or before the dt last Monday. This is not the NLS parameters.

    Of course, I can't test without some sample data and the exact results you want from these data. You may need a little something more If start_date and end_date are both on the same Sunday.
    Whenever you have a problem, please post a small example of data (CREATE TABLE and only relevant columns, INSERT statements) of all of the tables involved.
    Also post the results you want from this data, as well as an explanation of how you get these results from these data, with specific examples.
    Always tell what version of Oracle you are using.
    See the FAQ forum {message identifier: = 9360002}

  • How to find the difference "within weeks" between two date values?

    Hi all

    Jdev version 11.1.1.7.1

    I used two < af:inputDate > & a < af:inputText > < af:panelFormLayout > components. My requirement is, I want to display the difference of weeks between these two day values in the inputText component when the user clicks the shape.

    Any suggestion would be appreciated.

    Kind regards


    Novel

    You can use this method to get the number of days between the day and date, and then divided by 7 to get the number of weeks

    public static long getDifferenceDaysBetweenTwoDates (d1, d2 oracle.jbo.domain.Date oracle.jbo.domain.Date)

    {

    If (d1! = null & d2! = null)

    {

    return (d1.getValue () .getTime () - d2.getValue () .getTime ()) / (24 * 60 * 60 * 1000);

    }

    return 0;

    }

    Check - http://sameh-nassar.blogspot.in/2014/10/dealing-with-dates-in-java.html

    Ashish

  • How to find the difference between two dates in the presentation layer

    Hi gurus,

    Hello to everyone. Today, I came with the new requirement.


    I need to know the difference between a date and the current date in the formula column application presentation layer.by.



    Thank you and best regards,
    Prates

    Hi Navin,

    TIMESTAMPDIFF function first determines the timestamp component that corresponds to the specified interval setting. For example, SQL_TSI_DAY corresponds to the day component and SQL_TSI_MONTH corresponds to the component "month".

    If you want to display the difference between two dates in days using SQL_TSI_DAY, unlike butterflies SQL_TSI_MONTH and so on...

    hope you understand...

    Award points and to close the debate, if your question is answered.

    See you soon,.
    Aravind

  • How to find the difference between the two timestamp columns

    Dear all,
    Please solve my problem,
    I have Table name of the folder that has the following columns,
    EmpID in the number column, timestamp dat
    who has the following values
    Expand | Select | Wrap | Line numbers
    EmpID dat
    ====== ====
    101 4/9/2012 09:48:54
    101 4/9/2012 09:36:28
    101 4/9/2012 18:16:28
    101 4/10 / 2012 09:33:48
    101 4/10 / 2012 12:36:28
    101 4/10 / 2012 20:36:12
    101 4/11 / 2012 09:36:28
    101 4/11 / 2012 16:36:22
    Here, I need to display the following columns

    EmpID, min (DAT) as start, max (dat) as end and difference (max (dat) - min (dat) for each day,
    for example,.
    End difference EmpID Strart
    101 4/9/2012 09:48:54 09/04/2012 18:16:28 8.28
    Like this.
    3 days different here is so it should return 3 discs with the mentioned columns above,
    Please help me find it.

    Thank you
    Kind regards
    Gurujothi

    Published by: Gurujothi on April 25, 2012 04:45

    Gurujothi wrote:
    Dear Alex,
    Thank you for your reply,
    Depending on whether you are your quesy showing it in hour and minutes format its right but you mention all rows of the query
    If the table has more than 3000 lines then how it is possible to write this much higher request,
    my table with almost 3500 lines.

    You can make a few changes in the above query I posted the last I mentioned?

    Thank you.

    Published by: Gurujothi on April 25, 2012 23:18

    Hello

    You don't need to write whole records 3000 it... alex just wrote that for example have no table as your table...
    so just use the under part of this query as in the previous post, alex:

    select empid, trunc(dat),
           min(dat) as start_dat,  to_char(min(dat), 'hh24:mi:ss') as start_dat_part,
           max(dat) as end_dat, to_char(max(dat), 'hh24:mi:ss') as end_dat_part,
           max(dat)-min(dat) diff, to_char(max(dat)-min(dat), 'hh24:mi:ss') diff_part,
           extract(day from max(dat)-min(dat)) || ' days'
           || ':' ||  extract(hour from max(dat)-min(dat)) || ' hours'
           || ':' ||  extract(minute from max(dat)-min(dat)) || ' mins'
           || ':' ||  extract(second from max(dat)-min(dat)) || ' sekas'
    from trans
    group by empid, trunc(dat)
    

    hope this fixes your need

  • How to get the difference in dates in days

    Hi friends,

    How to get the date difference between two dates exactly

    date2-date1 days exactly... shouyld rounded to the nearest value of...

    and one entire display i mean positive integer
    Select round ((sysdate) - to_date (date_start)), date_start days of per_periods_of_service

    This gives negative also result

    Hello

    776317 wrote:
    Hi friends,

    How to get the date difference between two dates exactly

    date2-date1 days exactly... shouyld rounded to the nearest value of...

    Date1 - date2 is the exact number of days is after date2 date1. (It is as accurate as possible, given that the DATEs do not have fractions of a second).

    and one entire display i mean positive integer
    Select round ((sysdate) - to_date (date_start)), date_start days of per_periods_of_service

    You want to probably get the exact number of days first, then ROUND this number:

    SELECT  ROUND (SYSDATE - TO_DATE (date_start, ...))   AS days
    FROM    per_periods_of_service; 
    

    Always pass arguments at least 2 to TO_DATE:
    (1) the string to be converted, and
    (2) a saying string how (1) is formatted

    This gives negative also result

    Right; If date1 is before date2 then date1 - date2 returns a negative number.
    If you want to never get a negative value, use ABS (date2 - date1). If it returns 4, then you know that one of the DATEs was 4 days before the other, but you won't know who was earlier.

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) and also publish outcomes from these data.
    Explain, using specific examples, how you get these results from these data.
    Always tell what version of Oracle you are using.

  • How to find the difference between related areas

    Hi all
    My problem is I have a table in which each woman will be visited maximum 8 times, each woman may or may not
    given a reference in any visit which in turn women will respond to this referral in a next visit
    (not necessarily the next visit), I need to calculate the average number of visits between the visit
    in which the wife received a refferal and visit in which she responds.

    as an example

    woman_id visit_id given_referral respond_to_referral
    -------- -------- -------------- -------------------
    1 1 TRUE NULL
    1 2 TRUE TRUE
    1 3 TRUE NULL
    1 4 TRUE NULL
    1 5 NULL NULL
    1 6 TRUE NULL

    This means that visit 2 response is referral to visit 1 so the difference is 1.
    and the visit 6 response corresponds to the referral in visit 4 so the difference is 4.
    what I can't do is knowing how to know answer to visit 6 associated reference visit 4 No 1 or 2 or 3...

    and I need to find the following table of the foregoing

    woman_id difference between referral and response
    ---------          ---------------------------------------
    1                    1
    1                    2

    quick access to help please

    Published by: M.Jabr on December 14, 2009 08:05
    with t as (
               select 1 woman_id,1 visit_id,'TRUE' given_referral,NULL respond_to_referral from dual union all
               select 1,2,'TRUE','TRUE' from dual union all
               select 1,3,'TRUE',NULL from dual union all
               select 1,4,'TRUE',NULL from dual union all
               select 1,5,NULL,NULL from dual union all
               select 1,6,NULL,'TRUE' from dual
              )
    select  woman_id,
            diff
      from  (
             select  woman_id,
                     visit_id - last_value(case
                                             when given_referral is not null
                                               then visit_id
                                           end
                                           ignore nulls
                                          )
                                over(
                                     partition by woman_id
                                     order by visit_id
                                     rows between unbounded preceding and 1 preceding
                                    ) diff,
                     respond_to_referral
               from  t
            )
      where respond_to_referral = 'TRUE'
    /
    
      WOMAN_ID       DIFF
    ---------- ----------
             1          1
             1          2
    
    SQL> 
    

    SY.

  • How to find the most recent date prev?

    Hello

    Here is the sample table and sample data.
    CREATE TABLE t1 (code NUMBER, dt DATE, price NUMBER)
    /
    Insert into T1 (CODE, DT, PRICE) Values (1, TO_DATE('10/21/2012', 'MM/DD/YYYY'), 10);
    Insert into T1 (CODE, DT, PRICE) Values (1, TO_DATE('10/22/2012', 'MM/DD/YYYY'), 10);
    Insert into T1 (CODE, DT, PRICE) Values (1, TO_DATE('10/23/2012', 'MM/DD/YYYY'), 12);
    Insert into T1 (CODE, DT, PRICE) Values (1, TO_DATE('10/24/2012', 'MM/DD/YYYY'), 9);
    Insert into T1 (CODE, DT, PRICE) Values (1, TO_DATE('10/25/2012', 'MM/DD/YYYY'), 8);
    Insert into T1 (CODE, DT, PRICE) Values (1, TO_DATE('10/26/2012', 'MM/DD/YYYY'), 13);
    Insert into T1 (CODE, DT, PRICE) Values (1, TO_DATE('10/27/2012', 'MM/DD/YYYY'), 11);
    Insert into T1 (CODE, DT, PRICE) Values (1, TO_DATE('10/28/2012', 'MM/DD/YYYY'), 15);
    Insert into T1 (CODE, DT, PRICE) Values (2, TO_DATE('10/29/2012', 'MM/DD/YYYY'), 15);
    Insert into T1 (CODE, DT, PRICE) Values (1, TO_DATE('10/30/2012', 'MM/DD/YYYY'), 13);
    Insert into T1 (CODE, DT, PRICE) Values (1, TO_DATE('10/31/2012', 'MM/DD/YYYY'), 13);
    COMMIT;
    I have to read the table so that I got when I read a line of CODE = 1 for DT = 30 - OCT - 2012, I have to check what was the price for the CODE = 1 in the most recent previous date (which is October 28, 2013). Then I will compare the two prices (for 30 Oct 28th) and fill out another table.

    I hope I have described the situation well.

    Could someone help me with this please?

    My actual table contains a few million lines in DEV env and a lot more in PROD.

    Thanks in advance!

    If you use the analytical function LAG you can 'Watch' data of previous lines...

    SQL> ed
    Wrote file afiedt.buf
    
      1  select code, dt, price
      2        ,lag(price) over (partition by code order by dt) as prev_price
      3* from t1
    SQL> /
    
          CODE DT                        PRICE PREV_PRICE
    ---------- -------------------- ---------- ----------
             1 21-OCT-2012 00:00:00         10
             1 22-OCT-2012 00:00:00         10         10
             1 23-OCT-2012 00:00:00         12         10
             1 24-OCT-2012 00:00:00          9         12
             1 25-OCT-2012 00:00:00          8          9
             1 26-OCT-2012 00:00:00         13          8
             1 27-OCT-2012 00:00:00         11         13
             1 28-OCT-2012 00:00:00         15         11
             1 30-OCT-2012 00:00:00         13         15
             1 31-OCT-2012 00:00:00         13         13
             2 29-OCT-2012 00:00:00         15
    
    11 rows selected.
    
  • Groovy Script to find the difference between Dates

    I am trying to build a script that takes a date from a RequestField and calculates the number of days between this date and the current date (from session).

    So far, I have a problem, the analysis to-date from the RequestField chain. I found this code but it doesn't seem to work...

    date = new Date().parse('yyyy/MM/dd', '1973/07/09')
    

    I get the following error...

    Error Message: groovy.lang.MissingMethodException: No signature of method: java.util.Date.parse() is applicable for argument types: (java.lang.String, java.lang.String) values: {"yyyy/MM/dd", "1973/07/09"}
    

    Any help would be greatly appreciated.

    Hello

    What version of server? The extract you have here works fine on mine (563).

    This also works for me

    new java.text.SimpleDateFormat('yyyy/MM/dd').parse('1973/07/09')
    

    See you soon

    Nils

  • How to find the broken blob data

    Hello

    I am using oracle 10G express edition and Oracle Apex 4.1 front-end, have created a table with structure below.

    > > CREATE TABLE 'HR_EMPLOYEE_DETAILS '.
    > > ('ID' NUMBER ENABLE NOT NULL,)
    > > 'HR_ID' ENABLE NUMBER NOT NULL,
    > > 'PHOTO_BLOB' BLOB,
    > > "MIME_TYPE" VARCHAR2 (64)
    (> >)


    I wrote a program sqlloader to load all the images in the table above, there are a few rows of data where the photos have not get uploaded, which shows the user but photo id is empty, how to identify such lines. Please suggest me

    Thank you
    Sudhir

    Your photo_blob column can have NULL values, in this case you can find them with something like:

    select id
      from hr_employee_details
     where photo_blob is null
    

    Or the photo_blob was filled with a LOB Locator is valid but contains no data. These cases can be found with something like:

    select id
      from hr_employee_details
     where dbms_lob.getlength(photo_blob) = 0
    

    Or you could have a case where some of the bytes actually enter the blob, but not all resulting in an invalid image. Who is going to be more difficult, try so the other two cases first ;-)

Maybe you are looking for

  • The videos show only the upper-left corner of the image during playback.

    When I try to play a video in Firefox (23.0) on my Mac, all I can see is the fourth upper left of the video image. When I try to display the same video in Safari it works fine. The entire image is rendered. Mac OS X 10.8.4

  • My yahoo page loads but is not formatted correctly with FF 15.0.1. How to fix?

    After the upgrade to Firefox 15.0.1, my yahoo homepage no longer matches the screen. All the information is there, but the format is not correct, and the page is too big for the screen. I can reduce the size of the page to the screen, but the spacing

  • 57 a KB2483110 code update

    Windows Update tries to update the junk e-mail Filter Outlook 2007 (KB2483110) but I get a 57. I am running Windows 7 with 64 bit. Thank you

  • How do you support users without having them reveal their password

    I'm in the desk stand in great company. The head of our Department of COMPUTER security has recently published an email, across the enterprise, reminding the user is to never give their password for SOME reason any. It has not yet, but may impede our

  • Generate unrest of the declaration

    It's been a long day and although I am sure that this shouldn't be too hard, I'm currently completely blocked.In short:All this boils down to: transform a bunch of strings separated by commas in instruction, but all the trees make it impossible for m