Substacting two dates and get result in months

Hi Friends
I have to subtract two dates and get the difference in months. Here's the sample data:
select DATE '2009-10-01' Date_value, 111 Cust_id,  DATE '2009-10-22' Open_date from dual
union all
select DATE '2009-10-01', 112 , DATE '2009-10-15' from dual
union all
select DATE '2009-10-01', 113 , DATE '2009-10-5' from dual
union all
select DATE '2009-11-01', 111 , DATE '2009-10-22' from dual
union all
select DATE '2009-11-01', 112 ,DATE '2009-10-15' from dual
union all
select DATE '2009-11-01', 113 ,DATE '2009-10-22'  from dualAnd here’s my query to get the difference in month:
SELECT cust_id, date_value, open_date,
       ROUND (ABS (date_value - open_date) / 365 * 12) diff_in_months
  FROM tBut the output is unexpected:
CUST_ID     DATE_VALUE     OPEN_DATE     DIFF_IN_MONTHS
111     1/10/2009     22/10/2009     1
112     1/10/2009     15/10/2009     0
113     1/10/2009     5/10/2009     0
111     1/11/2009     22/10/2009     0
112     1/11/2009     15/10/2009     1
113     1/11/2009     22/10/2009     0Ideally, the output should be as per the following:
CUST_ID     DATE_VALUE     OPEN_DATE     DIFF_IN_MONTHS
111     1/10/2009     22/10/2009     0
112     1/10/2009     15/10/2009     0
113     1/10/2009     5/10/2009     0
111     1/11/2009     22/10/2009     1
112     1/11/2009     15/10/2009     1
113     1/11/2009     22/10/2009     1What is wrong in my query?

Hi Centinul
Found the resolution. When I use the following query, I get the perfect result:
SELECT cust_id, date_value, open_date,
       ROUND (ABS (MONTHS_BETWEEN (open_date, date_value))) months,
       ROUND (ABS (date_value - open_date) / 365 * 12) diff_in_months,
       MONTHS_BETWEEN (date_value,ADD_MONTHS (LAST_DAY (open_date), -1) + 1 ) final_val
  FROM tAnd here's the output of all three columns:
CUST_ID     DATE_VALUE     OPEN_DATE     MONTHS     DIFF_IN_MONTHS     FINAL_VAL
111     1/10/2009     22/10/2009     1     1     0
112     1/10/2009     15/10/2009     0     0     0
113     1/10/2009     5/10/2009     0     0     0
111     1/11/2009     22/10/2009     0     0     1
112     1/11/2009     15/10/2009     1     1     1
113     1/11/2009     22/10/2009     0     0     1

Similar Messages

  • Subtract   two dates to get   months and days

    I havew to subtract two dates and get the differnce ,
              Date issuedDate= myobj.getIssueDate();
              Date expirationDate=DateUtils.addMonths(issuedDate, 6);
              long timeDiff=expirationDate.getTime()-new Date().getTime();
              long daysRemaning=timeDiff/86400000;this is code I get the number of days between the issue date and todays date.now my client wants not just days but months and days , i need suggestions on how to get days and months between any two given dates

    I have read that it is best to use java.util.Calendar for almost any time related issue.
    With some switch statements and a for loop, I think the Calendar class could solve the problem.

  • Substracting dates to get difference in months

    I want to subtract two dates and get the number of months between them. I don't care about the day. I just care about the month and year of the two dates. So in example 1, the difference between 01/2004 and 10/2003 is 3 months. Any ideas?
    Example 1:
    date1 = 10/15/2003
    date2 = 01/01/2004
    so.......
    date2 - date1 = 3 months
    Example 2:
    date3 = 08/07/2003
    date4 = 10/01/2003
    date4 - date3 = 2 months

    import java.util.GregorianCalendar;
    import java.util.Calendar;
    public class Test {
        public static void main( String args[] ) {
         GregorianCalendar gc1 = new GregorianCalendar(2003,9,15), //Yes, '9'
                     gc2 = new GregorianCalendar(2004,0,1); //Yes, '0'
         System.out.println((gc2.get(Calendar.YEAR)-gc1.get(Calendar.YEAR))*12+
                       gc2.get(Calendar.MONTH)-gc1.get(Calendar.MONTH));
    }

  • Get number of hours between two dates and two hours using factory calendar

    Hello all,
    I have the following requirement: I need to calculate the number of hours between two dates and two hours (start date- finish date and start hour-finish hour) or timestamps using a factory calendar. I must program it on CRM environment.
    Does anybody know a function module that makes it?
    Thanks in advance.
    Carmen

    Please check function module DURATION_DETERMINE.
    - April King

  • How do I store 4 hours of data and get it out of the "while loop" into a spreadshee​t file for documentin​g purposes? "See additional Text for additonal info"

    What a have is a VI that uses the following SUbvi's, starts with FP OpenVI, then FP Create TagVI, into a "While Loop" which contains a FP ReadVI outputting data into a Index ArrayVI outputting to a Display (DBL). This shows the output of a FP-AI-100 monitoring a 9v battery. I have to monitor this battery for a 4 hour period my problem is storing the 4 hours of data and getting it out of the "while loop" into a "Write to Spreadsheet File VI" all I seem to accomplish is just one data sample which I get into a spreed ship file with no problem. I just can't get 4 hours worth. By the way this is my first VI and I'm self
    trained so have mercy.

    I figured it out thanks.
    John Morris
    Glendinning Marine

  • How do I store 4 hours of data and get them out of the "while loop" into a spreadsheet file for documenting purposes? "See additional Text for additonal info"

    What a have is a VI that uses the following SUbvi's, starts with FP OPENvi, then FP Create Tagvi, into a "While Loop" which contains a FP READvi outputting data into a INDEX ARRAYvi outputting to a Display (DBL). This shows the output of a FP-AI-100 monitoring a 9v battery. I have to monitor this battery for a 4 hour period my problem is storing the 4 hours of data and getting it out of the "while loop" into a "Write to Spreadsheet File vi" all I seem to accomplish is just one data sample which I get into a spreed ship file with no problem. I just can't get 4 hours worth. By the way this is my first VI and I'm sel
    f trained so have mercy.

    I figured it out Thanks.
    John Morris
    Glendinning Marine

  • How do I insert new record and get results on a landing page

    how do I insert new record and get results on a landing page

    It's not clear from your post what you are asking. In a SQL database, you use the INSERT statement to insert a row into a table. You use the SELECT statement to retrieve rows. Here's some basic info on how to do that within PHP
    PHP MySQL Insert Into
    PHP MySQL Select

  • How describe model data and  get select in DB throw topLink.

    Hello,
    I have table from code
    create table t_tree
    id int primary key,
    parent_id int,
    value varchar2(255)
    Alter table t_tree
    add constraint constr_id_parent foreign key (parent_id) references t_tree (id)
    I must get query
    select level as lv,lpad('-@-', (level-1)*2)||value as MMM, t.* from t_tree t
    connect by prior id=parent_id
    start with T.PARENT_ID is null
    How describe model data and get select in DB throw topLink.
    Dema.

    So you'll probably have to write a function which uses dynamic SQL to retrieve the desired message text, like this untested one:
    CREATE OR REPLACE FUNCTION get_msg(p_db IN VARCHAR2,
                                       p_id IN NUMBER)
       RETURN VARCHAR2
    IS
       msg_txt  VARCHAR2(4000);
    BEGIN
       -- make sure p_db is a valid database link ...
       EXECUTE IMMEDIATE 'SELECT d_msg FROM msg@' || p_db || ' WHERE t_id = :id' INTO msg_txt USING p_id;
       RETURN msg_txt;
    EXCEPTION
       WHEN NO_DATA_FOUND THEN
          RETURN NULL;
    END get_msg;
    /The you can update likeUPDATE mex
       SET t_msg = get_msg(db_id, t_id);Hth, Urs

  • You know how you download apps, well let's say you have two devices and get an app on one and it goes to the other, how do you remove it from doing that?

    You know how you download apps, well let's say you have two devices and get an app on one and it goes to the other, how do you remove it from doing that?

    Click here and use the instructions to turn off the automatic downloads.
    (120206)

  • HT201272 I subscribed to National geographic and get billed every month but I never have been able to find or download each magazine. What am I missing?

    I subscribed to National Geographic and get billed every month for it but never have been able to find where to download my purchases  what am I missing?

    1. Reboot iPad
    2. Reboot router
    Note: Make sure your printer is on the same network as your iPadr (run CD to setup network printer)

  • Difference between two dates is required in years & months

    Hi all,
    <b>1.</b>   I want difference of two dates in years & months. For e.g. date1 is 29.09.1998 & date2 is 02.05.2001. I want date2-date1 to be calculated & displayed as  2.7 (yrs)in BEx.
    <b>2. </b>   Also, while calculating overalll result it should calculate as : 
        E.g.    2.7+ 2.6 = 5.1 (yrs)
    Right now I am getting difference in days. Is there any way to convert it in yrs & months.
    Note:string 'yrs' is not required
    Thanks in advance.
    Himanshu

    Hi,
    Reply by Arun was immensely helpful.
    To do calculation I used the following formula.
    TRUNC ( 'DATE DIFFERENCE' / 365 ) + TRUNC ( FRAC ( 'DATE DIFFERENCE' / 365 ) * 12 ) / 100
    where 'DATE DIFFERENCE' is difference of two dates in days (Employment start date - employment end date). By this I am getting number of days in years & months.

  • Howto: comparing 2 dates and getting the seconds

    By comparing 2 dates I need to extract or get somehow the elapsed seconds. I need to get only
    the seconds.
    Dates are in this format "hh:mm:ss"
    I tried with Date and GregorianCalendar but I couldnt get the desired result.
    Can someone provide me with a sample code please.

    remus.dragos wrote:
    Its not broken, nor absurd or complicated. I only need resolution only for minutes. The method that I use does not need to know more than 20 seconds.
    But I added minutes knoledge aniway. As you can see its easy to add hour resolution if needed. But I didnt needed it so thats why I took that approach.
    This looks cleaner for you? I just wrote it here, untested, but should work.
    public int returnSeconds(Calendar c1, Calendar c2)
    int mins =  c1.get(Calendar.MINUTES) - c2.get(Calendar.MINUTES);
    int secs =  c1.get(Calendar.SECONDS) - c2.get(Calendar.SECONDS);
    if (mins > 0) return secs+=mins;
    else            return secs;
    }And why should I treat all the time as miliseconds since the epoch. Thats an absurd afirmation without a detailed explanation.
    Edited by: remus.dragos on Apr 30, 2008 6:18 AM- Unnecessary creation of expensive Calendar objects.
    - Significantly slower
    - Breaks across Timezones
    - Unnecessary decision making introduces complexity
    - Introduces a bug by assuming c1 represents a time greater than c2
    - Introduces a bug by not multiplying minutes by 60 to obtain seconds.
    - Makes assumptions about the calendar system in use
    - Less maintainable because it limits the scope of time unit resolution handled by the logic
    I probably missed some more too. None of this is needed, and it is very brittle. Your problem can be solved by subtracting one fixed-point number from another. Go with es5f's approach.

  • BI- Extract Hour:Min from date and get Avg

    Hi there,
    am working on BI answer and trying to get the average of the working hours of the departments. The Working Hrs column is Date format in the Database. And I managed with Forum help to Extract the Hours from this column and do the average through the BMM in the RPD on the Fact table as following:
    EXTRACT( HOUR FROM "Ain_Access"."Fact_Access"."Working Hours")
    Now on the BI Answer results I have 8 instead of 08:57.
    But what I need is to Extract the Hour and the minuets and get their AVG.
    Appreciate your help,

    Averaging column with date/time format in BI Answer
    this expression will work:
    (60 * EXTRACT( HOUR FROM "Ain_Access"."Fact_Access"."AVG Working Hours" ) + EXTRACT( MINUTE FROM "Ain_Access"."Fact_Access"."AVG Working Hours" )) / 60
    but only if this "AVG Working Hours" is DATE type.... and this look like strange name fore DATE? "AVG" is average, aren't?
    Arent u need to use "Working Hours" instead of "AVG Working Hours" ?
    like this:
    (60 * EXTRACT( HOUR FROM "Ain_Access"."Fact_Access"."Working Hours" ) + EXTRACT( MINUTE FROM "Ain_Access"."Fact_Access"."Working Hours" )) / 60
    all best
    phenix

  • Comparing 2 dates and putting results in predetermined buckets

    Hi all
    I want to write a method which takes in as parameters 2 date objects. It works out the difference (number of years) between the 2 dates and depending on this the results are flagged against 1 of 5 buckets.
    Each bucket represents the following number of years 2, 5, 10, 20 and 30. So if my first 2 dates are taken and the number of years is 23 then the date should be stored against the 20 year bucket, or if the number of years is 6 then the date should be stored against the 5 year bucket, a value of 16 would be stored in the 20 year bucket etc...
    I was looking for suggestions on the neatest way of doing this - any suggestions would be appreciated.

    Sorry my original post wasnt to clear.
    When 2 dates are compared and the result is given e.g. 22 years - I need to add the second of the 2 dates to a container of some sort which represents 20 year things. So if the 2 dates are 01-Jan-2000 and 01-Jan-2022 then I would work out the difference in years which is 22 in this case, then the second date would be allocated to a bucket (an object which I havent decided on yet - e.g. could be a List or anything else) which hold dates which are closest to 20 years.
    As in this example the difference is 22 years (not exactly 20) the logic would allocate the date to the 20 year bucket as 22 is closer to any of the other buckets:
    2, 5, 10, 20 and 30. So I guess part of the logic is getting the difference in years e.g. 22 and then working out which of the 5 buckets this falls into.
    I hope that makes it clearer - thanks again.

  • Ipod Touch shows two dates and times on the sleep screen/locked screen

    First off i have the new ipod touch and i got it this christmas.since a couple weeks ago on the sleep screen/locked screen(the screen that appears when you hit the button on top of ipod) it shows the current date and time but in the background of that time it also sais 7:12 January 6 2010. It has said that for about two weeks. Also i noticed that when you slide the unlock bar over you can still see the slider in the background at the original position. One last thing has been happening with the volume when listening to music. When i try to turn it up i slide the volume button to the right to make it louder and when i let got it just slides back and does not get louder. What is going on.

    I had the same problem with double image of date and time and also the unlock arrow. I found out that the problem was that I had, without knowing it, taken a snapshot of my wallpaper screen and saved it back to my saved photos. When I used that wallpaper image again, there was the dual image of time and date. All I had to do was change wallpaper! Good luck!

Maybe you are looking for