Difference of two dates in the month

I need to find the difference between two dates as the number of months:
find the last day of the month of the two dates. subtract and give the difference in number of months.

user637544 wrote:
I need to find the difference between two dates as the number of months:
find the last day of the month of the two dates. subtract and give the difference in number of months.

Why not just use MONTHS_BETWEEN function? That does not help?

SQL> SELECT sysdate FROM Dual;

SYSDATE
---------
01-JUN-09

SQL> SELECT MONTHS_BETWEEN (SYSDATE,
  2                         TO_DATE ('01/01/2009', 'DD/MM/YYYY')
  3                        ) diff_in_months
  4    FROM DUAL;

DIFF_IN_MONTHS
--------------
             5

SQL>

You can also use the LAST_DAY function to get the last day of the month.

SQL> SELECT LAST_DAY(sysdate) FROM Dual;

LAST_DAY(
---------
30-JUN-09

SQL> SELECT MONTHS_BETWEEN (LAST_DAY(SYSDATE),
  2                         LAST_DAY(TO_DATE ('01/01/2009', 'DD/MM/YYYY'))
  3                        ) diff_in_months
  4    FROM DUAL;

DIFF_IN_MONTHS
--------------
             5

SQL>

Kind regards
JO

Published by: Joice John - added LAST_DAY function example

Tags: Database

Similar Questions

  • difference between two dates in the year/month/day in Obiee

    Hi gurus,

    I know this question have been asked and answered several times but I have a requirement that is a little different, then the previous ones.

    I want to calculate the difference between two dates in OBIEE10g in year/month/day format similar to the below SQL output

    SQL > select end_date, start_date,

    trunc (months_between (end_date, start_date) / 12) years.

    months of mod (trunc (months_between (end_date, start_date)), 12).

    End_date - add_months (start_date, trunc (months_between (end_date, start_date))) days

    t

    Thanks in advance

    SK

    Search for this

    "TIMESTAMPDIFF IN THE FORM OF MTHS # YEARS."

    or else

    To get the current mandate of employees since the date of hiring in the form of # years # mths or # year (s) # month (s)

    Concat (concat (cast (TIMESTAMPDIFF (SQL_TSI_Month, "Employee attributes". (((' ' Hiring last Date employee ", CURRENT_DATE) / 12 as char),"Year (s)"), concat (cast (MOD (TIMESTAMPDIFF (SQL_TSI_Month,"Employee attributes". (((((' ' Hiring last Date employee ", CURRENT_DATE), 12) as char), 'Month (s)'))

    Concat (concat (CAST (TIMESTAMPDIFF (SQL_TSI_YEAR, "Employee attributes". (((' ' Hiring last Date employee ", CURRENT_DATE) as CHAR),"Year (s)"), concat (cast (MOD (TIMESTAMPDIFF (SQL_TSI_Month,"Employee attributes". (((((' ' Hiring last Date employee ", CURRENT_DATE), 12) as char), 'Mth (s)'))

  • 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

  • Difference between two dates in the server behaviors

    Hello all,.

    I am trying to create a Recordset by using server behaviors and that I'm not an expert in php, I'm completely lost...

    The problem is this: I want to find in a table of all the records where the difference between today's date and date of creation (stored in this table, although course) is superior to, say 30 days. How can I specify the variable field?

    Thank you very much in advance for your help!

    You must use a sql like query below:

    SELECT * FROM table WHERE creationDate< date_sub(now(),="" interval="" 30="">

    'table' is the name of the table that you want to select the information of and 'creationDate' being the column 'date' in the database

  • How do the time difference between two dates?

    Hi all

    I use this query to get the time difference between two dates.

    Select to_timestamp ('2012-10-03 12:00 ',' YYYY-MM-DD hh)-to_timestamp ('2012-10-03 11:00 ',' YYYY-MM-DD hh) as double diff;

    but do not get the correct result.

    Thank you

    Left KEY... Left Padding of tanks.

  • Dynamic action - Get the difference between two dates + times

    I have problems a little dynamic to work action. I'm trying to get the time between two dates with the time difference.

    Here is what I got (this is apex 4.0):

    Two date pickers + two numbers fields (date/start/end times)

    I created a dynamic action on the page who fires on the point lose focus (above points).

    The real action for the DA is the body of the PL/SQL function:
    declare
      end_date DATE;
      start_date DATE;
    Begin
      start_date := to_char(:P1_START_DATE || ' ' || :P1_START_TIME, 'DD-MON-YYYY HH:MIAM');
      end_date := to_char(:P1_END_DATE || ' ' || :P1_END_TIME, 'DD-MON-YYYY HH:MIAM');
      :P1_HOURS := end_date-start_date;
    End;
    When I change the values on the page, I get the following error:

    AJAX call back Server error ORA-06502: PL/SQL: digital or value error: character number conversion error to set the value.

    I'm guessing that there is a problem with the date formatting, but I can't make it work. Thanks in advance!

    Hi djston,

    because you chose the dynamic action of 'Set value' with the "Body of the PL/SQL function" type you need to return the value. Try the following code

    declare
      end_date DATE;
      start_date DATE;
    Begin
      start_date := to_date(:P1_START_DATE || ' ' || :P1_START_TIME, 'DD-MM-YYYY HH:MIAM');
      end_date := to_date(:P1_END_DATE || ' ' || :P1_END_TIME, 'DD-MM-YYYY HH:MIAM');
      RETURN (end_date-start_date)*24;
    End;
    

    and P1_REQUESTED_HOURS like 'item affected. "

    Concerning
    Patrick
    -----------
    My Blog: http://www.inside-oracle-apex.com
    APEX 4.0 Plug-Ins: http://apex.oracle.com/plugins
    Twitter: http://www.twitter.com/patrickwolf

    Published by: Patrick Wolf on January 17, 2011 10:54

  • How to tell the difference between two dates

    Hello:

    I have a question on how to get the difference in quantities for two dates in the replies and dashboard.

    This is the scenario. I have two guests date and three columns. The first column must be the sum of the amounts for the first quick date. The second column should be the sum of the amounts for the second fast date. And the third column is the difference between the first two columns.

    Basically, I'm trying to do something similar to a function of sum of two lines but trying to get the difference between the two rows. Any help to solve this problem would be appreciated.

    Use the COLUMN FILTERS level...

    the first column with the first variable of date quick presentation of the filter

    Similarly, the second column in the second variable of quick overview of date filter

    Kind regards
    Rambeau

  • Find the difference between two date and time

    Hi friends,

    I wanted to find the difference between two date and time, but my qury is slightest error "invalid number."

    select sql_step_num,proc_name,run_seqno,start_date,end_date,(to_char(start_date,'HH24-MI-SS') - to_char(end_date,'HH24-MI-SS') ) as ed  
    from eval.EVAL_RUNTIME_DETAILS
    where trunc(start_date) = trunc(sysdate) 
    order by sql_step_num;

    You try to get the feel between two char strings.
    And more difference between two dates gives a NUMBER of days.
    Try this:

    select sql_step_num,proc_name,run_seqno,start_date,end_date,numtodsinterval(end_date-start_date,'DAY') as ed
    from eval.EVAL_RUNTIME_DETAILS
    where trunc(start_date) = trunc(sysdate)
    order by sql_step_num;
    
  • retrieve the last date of the month

    Hello

    I give myself a date. I want to find the last date of the month containing the given date.

    Is there any built-in why?

    Thanks in advance.

    DateTimeUtilities.getNumberOfDaysInMonth (cal.get (Calendar.MONTH), cal.get (Calendar.YEAR));

  • First date of the month

    Hi experts,

    Please let me know how to get registered for the 1st of the month by using the query, after passing a date in the query:

    My queries below give me 1 license of the year and the last date of the month... Kindly hep...
    SELECT TRUNC(TO_DATE('24-JUN-2011'),'YEAR') FROM DUAL
    
    SELECT LAST_DAY(ADD_MONTHS('24-JUN-2011',12 - 
    TO_NUMBER(TO_CHAR(SYSDATE,'mm')))) FROM DUAL
    Help appreciated,

    Thnx in advance
    TRUNC(TO_DATE('24-JUN-2011'),'MM')
    

    And he always will be always 1... ;)

  • I want to generete list date of the month

    I write this sentence to generete list date of the month*.

    Select TO_CHAR (TRUNC (SYSDATE, 'month'),
    "fmday jj/mm/rrrr.
    ) first_day_cur_month,.
    To_char (LAST_DAY (TRUNC (SYSDATE, 'month')) + 1-1 / 86400)
    ,
    last_day_cur_month "fmday jj/mm/rrrr")
    of the double

    but it works like the need of AI
    can be function or peocedure
    How to fix the sentence as dates list the month generete
    Please please

    Try this:

    SQL> ed
    Wrote file afiedt.buf
    
      1  SELECT TRUNC(SYSDATE,'MM') + lvl FROM
      2  (SELECT level - 1 lvl
      3  from dual
      4* connect by level <= (TRUNC(LAST_DAY(SYSDATE)) - TRUNC(SYSDATE,'MM')) + 1)
    SQL> /
    
    TRUNC(SYS
    ---------
    01-AUG-10
    02-AUG-10
    03-AUG-10
    04-AUG-10
    05-AUG-10
    06-AUG-10
    07-AUG-10
    08-AUG-10
    09-AUG-10
    10-AUG-10
    11-AUG-10
    
    TRUNC(SYS
    ---------
    12-AUG-10
    13-AUG-10
    14-AUG-10
    15-AUG-10
    16-AUG-10
    17-AUG-10
    18-AUG-10
    19-AUG-10
    20-AUG-10
    21-AUG-10
    22-AUG-10
    
    TRUNC(SYS
    ---------
    23-AUG-10
    24-AUG-10
    25-AUG-10
    26-AUG-10
    27-AUG-10
    28-AUG-10
    29-AUG-10
    30-AUG-10
    31-AUG-10
    
    31 rows selected.
    
    SQL> 
    
  • 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}

  • Difference between two dates

    Hello

    I have two columns both of them date data types.

    I would like to know the difference between these two dates.

    SELECTION INTERVAL ' + 2342 23:23:23 ' DAY (4) TO THE SECOND - INTERVAL ' + 102 13:13:13 ' DAY (4) SECOND FROM DUAL;

    But, I get this:

    '+ 2240 10:10:10.000000'

    I'd like to see the result in a year-month and jour-deuxieme groups. I can't do anything with + 2240 days...

    Thanks in advance!

    Hello Adme12

    You can use it.

    SET LINESIZE 110 PAGESIZE 20

    COLUMN today FORMAT A20

    COLUMN TOMORROW FORMAT A20

    COLUMN YEAR_MONTHS FORMAT A15

    COLUMN DAY_SEC FORMAT A30

    SET ECHO ON

    SELECT TO_CHAR (today, ' JJ.) MR. HH24:MI:SS' YYYY) LIKE TODAY

    , TO_CHAR (tomorrow, ' JJ.) MR. HH24:MI:SS' YYYY) TOMORROW

    NUMTOYMINTERVAL (MONTHS_BETWEEN (TOMORROW, TODAY), 'MONTHS') AS YEAR_MONTHS

    , NUMTODSINTERVAL (TOMORROW - ADD_MONTHS (TODAY, TRUNC (MONTHS_BETWEEN (TOMORROW, TODAY))), 'DAY') AS DAY_SEC

    Of

    (SELECT SYSDATE + INTERVAL ' + 102 13:13:13 ' DAY (4) ON THE SECOND THAT TODAY ' HUI)

    , SYSDATE + INTERVAL ' + 2342 23:23:23 ' DAY (4) ON THE SECOND TOMORROW

    OF THE DOUBLE

    )

    ;

    TODAY TOMORROW YEAR_MONTHS DAY_SEC

    -------------------- -------------------- --------------- ------------------------------

    23.04.2014 04:49 10.06.2020 14:59:10 + 000000006-02 + 000000018 10:10:10.000000000

    Of course, it is not so good that you have so many 0, but you can convert the result to a char (TO_CHAR) and you can enter.

    I hope this helps you!

    Kind regards

    David

  • Difference between two data aquistion

    Hello

    I got a data aquistion system that is supposed to keep data acquisition selection until the difference between two points a lot. For example, I get voltage (mv) as 2.31, 2.32, 2.34. 2.33 2.32, 2.33 2.32. the data up to a point, say 0.1V higher or lower than the first. Thewhole procss stop. Thank you for your suggestions.

    Try this... (Labview 8.2)

  • Do not have precisely the difference ECCAS current date and the date, we pass to the function.

    Hello

    I did a feature where when I go to any expiry date that I must check with the current date... and get the difference in days.

    When I change the value to this fuction 2012-01-05 and my current date is 2011-12-22 that I am getteing diffDays = 45, when I pass 2011-12-25 and my current date is 2011-12-22, then I'm diffDays = 34.

    I didn't know that's why 31 days added in my diffDays? If there are 31 days month so it will add 31 and if the month is 30 we can add 30 days? I didn't know where I am going wrong. I enclose my code that I use. Pls check and tell me where I'm wrong...

     public static String isBookExpired(String date) {
                System.out.println("**********");
                date = date.trim();
                int index = date.indexOf(" ");
                if(index != -1) {
                    date = date.substring(0, index);
                }
                    String strArr[] = stringtoArray(date , "-" );
    
                Calendar substribtionDt = Calendar.getInstance();
                //myCalendar.
                substribtionDt.set(Calendar.YEAR, Integer.parseInt(strArr[0]));
                substribtionDt.set(Calendar.MONTH, Integer.parseInt(strArr[1]));
                substribtionDt.set(Calendar.DATE, Integer.parseInt(strArr[2]));
    
                Calendar today = Calendar.getInstance();
    
                long diffDays = ( substribtionDt.getTime().getTime() - today.getTime().getTime()) / (24 * 60 * 60 * 1000);
    
                if(diffDays < 0) {
                    return "Yes";
                }
                else
                    return "No";
            }
    

    months in a calendar object index starts with 0 rather than 1 in the real world, you must set the value of the months in the calendar by subtracting by the date that you enter.

Maybe you are looking for

  • Satellite A200-1GC (PSAE3E) how to disable the WLAN switch?

    Hello everyone,is it possible to disable this stupid wlan switch, which prevents any new wlan card working properly? I tried to install several different wireless network cards: Intel 5100, Intel 5300 etc. Each card was working properly (Device Manag

  • acquisition of data bnc2120

    I have BNC-2120 OR with me. I set up a counter that counts the signal to CTR0Source. I posted the count each second implementation of a timer loop within a certain time. Now I need to get another source of entry (pulses) we'll say CTR1 Source input a

  • Updated continuously offers the same updates, which I've already refused

    I prefer to choose what updates I receive.  No, I never use "automatic updates".  I don't have Office 2007.  Whenever I turn on my computer updated tells my "new updates are available"... but it's the same update, I uncheck every time.  Is it possibl

  • Cannot read dvd, says that a compatible decoder is installed

    decoder for Windows media player I have windows XP Service pack 3. I'm playing commercial DVD discs using Windows Media Player, but I get a massage: Windows Media Player cannot play the DVD because no compatible DVD decoder is not installed on your c

  • Cover of Xperia z5

    Hi I just bought the real cover of Sony z5 premium. However, I can't figure out how to work the windows? I need to download a gadget to do that work? And what other features does the coverage?Thank you