Number of months in two dates

Hi Experts,

I want every month in two dates:

I have change in output.

Select TO_DATE (' 02/02/2003 ', ' dd/mm/yyyy') as start_date, TO_DATE (' 02/02/2003 ', ' dd/mm/yyyy') as double end_date

Description: I have a month (2)

Desired output

months of the year

----    -----

2 of 2003

Select TO_DATE (' 2003/02/25 ', ' dd/mm/yyyy') as start_date, TO_DATE (' 2003/03/01 ', ' dd/mm/yyyy') as double end_date

Description: I have two months (2.3)

Desired output

months of the year

----    -----

2 of 2003

3 2003

Select TO_DATE (' 28/12/2003 ', ' dd/mm/yyyy') as start_date, TO_DATE (' 2004/04/01 ', ' dd/mm/yyyy') as double end_date

Description: I have 5 months (2003:12, 2004: 1, 2, 3, 4)

Desired output

months of the year

----    -----

12 2003

2004 1

2 2004

2004 3

4 2004

Thanks in advance

BANNER

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

Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - production

Post edited by: Ràmio

This...

-Started test data

WITH qry AS

(select TO_DATE (' 02/02/2003 ', ' dd/mm/yyyy') as start_date, TO_DATE (' 02/02/2003 ', ' dd/mm/yyyy') as double end_date)

UNION ALL

Select TO_DATE (' 2003/02/25 ', ' dd/mm/yyyy') as start_date, TO_DATE (' 2003/03/01 ', ' dd/mm/yyyy') as double end_date

UNION ALL

Select TO_DATE (' 28/12/2003 ', ' dd/mm/yyyy') as start_date, TO_DATE (' 2004/04/01 ', ' dd/mm/yyyy') as double end_date

)

-Test data that ended

SELECT start_date,

End_date,

EXTRACT (YEAR OF ADD_MONTHS (start_date,(LEVEL-1))) year.

EXTRACT (MONTH FROM ADD_MONTHS (start_date,(LEVEL-1))) Lun

OF qry

CONNECT BY LEVEL<=>

AND PRIOR start_date = start_date

AND PRIOR DBMS_RANDOM. VALUE IS NOT NULL.

OUTPUT:-

========

START_DATE END_DATE YR LUN

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

2 FEBRUARY 03 2003 2 FEBRUARY 03 2

25 FEBRUARY 03 2003 1 MARCH 03 2

25 FEBRUARY 03 2003 1 MARCH 03 3

28 DECEMBER 03 2003 1 APRIL 04 12

28 DECEMBER 03 2004 1 APRIL 04 1

28 DECEMBER 03 2004 1 APRIL 04 2

28 DECEMBER 03 2004 1 APRIL 04 3

28 DECEMBER 03 2004 1 APRIL 04 4

8 selected lines

Thank you

Ann

Tags: Database

Similar Questions

  • Number of weeks between two dates that are in the same or different year

    Hello

    How to calculate the number of weeks between two dates?

    for example. "" "17-mar-2013' and June 27, 2013" or June 15, 2013 "and" 25-mar-2014


    Thanks in advance.

    lukx

    Hello

    Looks like your date range is incorrect.

    Please try the below.

    SQL >-front right

    SQL > select D2 - D1 "days."

    2 round ((D2-D1)/7.0) "weeks."

    3 round (Months_between (d1, d2), 0) 'months '.

    (select 4

    5 TO_DATE ('20121201', 'YYYYMMDD') D1, D2 TO_DATE ('20130116', 'YYYYMMDD')

    6 from two

    (7) date_range;

    Elapsed days weeks months

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

    46         7         1

  • Dynamic calculation of the number of days between two dates in a table

    Hello

    I'm working on request where I dynamically calculate the number of days between two dates in a table.

    The calculation must be dynamic, i.e., when I recover the Start_date and End_date and move to the field following (call_duration) in the same row, the difference must be calculated dynamically in this area and make sure the field read-only.

    APEX version: 5.0

    Hi BO123,

    BO123 wrote:

    Hello

    I'm working on request where I dynamically calculate the number of days between two dates in a table.

    The calculation must be dynamic, i.e., when I recover the Start_date and End_date and move to the field following (call_duration) in the same row, the difference must be calculated dynamically in this area and make sure the field read-only.

    APEX version: 5.0

    one of the way to do this by calling ajax on change of end_date.

    See the sample code given below to fetch the resulting duration and making the field read only after calculation

    Step 1: Change your page

    under CSS-> Inline, put the code below

    .row_item_disabled {
      cursor: default;
      opacity: 0.5;
      filter: alpha(opacity=50);
      pointer-events: none;
    }
    

    Step 2: Create on demand Ajax process I say CALC_DURATION

    Please check Procces Ajax, see line 6.7 How to assign a value to the variable sent by ajax call

    Declare
      p_start_date  date;
      p_end_date    date;
      p_duration number;
    Begin
      p_start_date  := to_date(apex_application.g_x01);
      p_end_date    := to_date(apex_application.g_x02);
    
       --do your calculation and assign the output to the variable p_duration
      select p_end_date - p_start_date into p_duration
        from dual;
    
      -- return calculated duration
      sys.htp.p(p_duration);
    End;
    

    Step 3: Create the javascript function

    Change your page-> the function and the declaration of the Global Variable-> put the javascript function

    You must extract the rowid in the first place, for which you want to set the time, see line 2

    assuming f06, f07 and f08 is the id of the start date, and end date columns respectively, and duration

    See no line no 8 how set the value returned by the process of ajax at the duration column

    Replace your column to the respective column identifiers in the code below

    function f_calulate_duration(pThis) {
      var row_id  = pThis.id.substr(4);
      var start_date = $('#f06_'+row_id).val();
      apex.server.process ( "CALC_DURATION", {
      x01: start_date,x02: $(pThis).val()
    }, { success: function( pData ) {
    // set duration to duration column
    $('#f08_'+row_id).val(pData);
    // disable duration column
    $("#f08_" + row_id).attr("readonly", true).addClass('row_item_disabled'); }
    });
    }
    

    Step 4: choose the end date call the javascript function

    Go to report attributes-> edit your Date column end-> column-> Attrbiutes element attributes-> put the code below

    onchange="javascript:f_calulate_duration(this);"
    


    hope this helps you,

    Kind regards

    Jitendra

  • Number of Saturdays between two dates

    Hello

    How can I get number of Saturdays between two dates, including the two dates?

    for example number of Saturdays between 09/05/2014 and 10/05/2014 is five

    Help, please

    I use oracle 10g

    SQL> with t
      2  as
      3  (
      4  select to_date('05/09/2014 ', 'dd/mm/yyyy') from_dt
      5       , to_date('05/10/2014 ', 'dd/mm/yyyy') to_dt
      6    from dual
      7  )
      8   select to_char(from_dt + (level-1), 'fmDay') day_
      9        , count(*) cnt
     10     from t
     11  connect
     12       by level <= to_dt - from_dt + 1
     13    group
     14       by to_char(from_dt + (level-1), 'fmDay');
    
    DAY_             CNT
    --------- ----------
    Saturday           5
    Thursday           4
    Tuesday            4
    Friday             5
    Wednesday          4
    Monday             4
    Sunday             5
    
    7 rows selected.
    
    SQL>
    
  • Months between two dates

    Hello

    I have two dates. start date and end date.

    I want to extract months between two dates

    months should be as

    JAN

    FEB

    MAR

    for example.

    Start - 1-jan-2014

    date of end - 31 - dec - 2014

    How can I do this?

    Thanks and greetings

    CORINE

    You should always use the function MONTHS_BETWEEN...

    for example

    SQL > ed
    A written file afiedt.buf

    1 with t as (select date '' 2014-01-01 as start_date date ' 2014-12-31' as double end_date)
    2  --
    3 end of test data
    4  --
    5. Select to_char (add_months (start_date, rownum-1), 'My') by month
    6 t
    7 * connect by rownum<=>
    SQL > /.

    LUN
    ---
    Jan
    Feb
    Mar
    Apr
    May
    Jun
    Jul
    Aug
    Ms
    Oct
    Nov
    Dec

    12 selected lines.

    And if you want that they display, separated by commas...

    SQL > ed
    A written file afiedt.buf

    1 with t as (select date '' 2014-01-01 as start_date date ' 2014-12-31' as double end_date)
    2  --
    3 end of test data
    4  --
    5 Select listagg (to_char (add_months (start_date, rownum-1), 'My'), ',') group by month (order by rownum)
    6 t
    7 * connect by rownum<=>
    SQL > /.

    MONTH
    -----------------------------------------------------------------------------------------------------------------
    Jan, Feb, Mar, Apr, may, June, July, August, Sept, Oct, Nov, Dec

  • JavaScript anomaly on the number of days between two dates

    Use ApEx 4.0, I found an anomaly in a javascript code that calculates the number of days between two dates, the current_date and the past_date. If the past and present is the or before March 10, 2013, and the current_date lies between 10 March 2013 and November 3, 2013, the number of days will be from 1 day to less than the actual number. Between November 3, 2013 and on 4 November 2013, the increments of number by 2, then the count will be accurate from this date forward.

    Here are some examples:

    March 10, 2013 = 69 days of December 31, 2012
    March 11, 2013 = 69 days of December 31, 2012
    March 12, 2013 = 70 days of December 31, 2012

    November 3, 2013 = 306 days in December 31, 2012
    November 4, 2013 = 308 days in December 31, 2012

    11 March should be 70 and 12 March should be 71. November 3 is 307 and 4 November corrects the number of fake, which began March 11.

    Change the past_date to March 10, 2013 produces the following:

    March 10, 2013 = 0 days of March 10, 2013
    March 11, 2013 = 0 days of March 10, 2013
    March 12, 2013 = 1 days of March 10, 2013

    But change the past_date to 11 March 2013, product of the correct numbers:

    March 11, 2013 = 0 days of March 11, 2013
    March 12, 2013 = 1 days of March 11, 2013
    March 13, 2013 = 2 days of March 11, 2013

    I would certainly all help to determine the cause of this anomaly. Here's the javascript code:

    var w1 = ($v ("P48_PAST_DATE"));
    W1 = (w1.toString ());
    vmon var = (w1.substr (3.3));
    vyr var = (w1.substr (7));
    var r = (vyr.length);
    If (r == 2)
    vyr. = (parseFloat (vyr) + 2000);
    vday var = (w1.substr (0.2));
    var y = (vmon.concat ("", vday, ",", vyr));
    y = Date.parse (y);

    var w2 = ($v ("P48_CURRENT_DATE"));
    var vmon2 = (w2.substr (3.3));
    var vyr2 = (w2.substr (7));
    var vday2 = (w2.substr (0.2));
    var x = (vmon2.concat ("", vday2, ",", vyr2));
    x = Date.parse (x);

    var numdays = (x - y);
    numdays = (Math.floor(numdays / 86400000));
    $s ("P48_NUMBEROFDAYS", numdays);

    Did you google for something like "javascript number of days between two dates. I think you will find the explanation to this observation:

    This method does not work correctly if there is an advanced economies jump between the two dates.

    There are examples available to calculate the difference between two dates.

  • Get the number of months between the dates.

    Hi I need to get the exact number of months between the dates. So if I'm Sysdate - to_date(31-JAN-2011) I should get something like 8.4 months. Months_between function doesn't quite give me what I need. Is it possible that I can do this?

    876056 wrote:
    Hi I need to get the exact number of months between the dates. So if I'm Sysdate - to_date(31-JAN-2011) I should get something like 8.4 months. Months_between function doesn't quite give me what I need. Is it possible that I can do this?

    I don't see how you get 8.4 months. The counted months are February, March, April, may, June, July, August and, as of today, 9 and change the days of September. Unless I forgot the lessons of counting, I learned in kndergarten is 7 full months and a bit.

    Maybe you want something like:

    SQL> select months_between(Sysdate, to_date('01-JAN-2011', 'dd-mon-yyyy')) mb,
      2         (Sysdate - to_date('01-JAN-2011', 'dd-mon-yyyy'))/30 div30
      3  from dual;
    
            MB      DIV30
    ---------- ----------
    8.27800142 8.38726813
    

    If you get the form date a table, you can use trunc (date_column, 'mm') at the date of the first day of the month.

    John

  • Display the months between two dates

    Hello:
    I need all the months between two dates. I entered two dates as January 15, 2001 and 2011-mar-03. I want the result as below.

    JANUARY 15, 2011 - JANUARY 31, 2011
    February 1, 2011 - February 28, 2011
    MARCH 1, 2011 - MARCH 3, 2011

    I held this result of sql query in oracle. Could you please suggest me?
    Thanks in advance.
    Abhishek
    SELECT     GREATEST(st, ADD_MONTHS(TRUNC(st, 'MONTH'), (LEVEL - 1))) dt,
               LEAST(ed, LAST_DAY(ADD_MONTHS(TRUNC(st, 'MONTH'), (LEVEL - 1)))) ld
          FROM (SELECT DATE '2011-01-15' AS st, DATE '2011-03-03' AS ed
                  FROM DUAL)
    CONNECT BY LEVEL <= MONTHS_BETWEEN(TRUNC(ed, 'MONTH'), TRUNC(st, 'MONTH')) + 1
      ORDER BY 1;
    
    DT                       LD
    ------------------------ ------------------------
    15.01.2011 00:00:00      31.01.2011 00:00:00
    01.02.2011 00:00:00      28.02.2011 00:00:00
    01.03.2011 00:00:00      03.03.2011 00:00:00
    

    HTH, Urs

  • Calculate the number of days between two dates

    Hello

    Can someone help please change my formcalc script to calculate the number of working days between two date fields.  My script currently calculates the total number of days between two dates, including the weekends which must be excluded from the total.

    If

    (HasValue (Start_Date1) & HasValue (End_Date1)) then

    $

    = Date2Num (End_Date1, "YYYY-MM-DD" "en_IE") - Date2Num (Start_Date1, "YYYY-MM-DD" "en_IE") + 1

    on the other

    ""

    endif

    Any help will be most appreciated.

    Thank you.

    Check...

    (1) you said that you put the script on the event «days1» calculate My sample imitates the variable names used in the original message, "Start_Date1" and "End_Date1". If the names of variables for the start and end dates are different, you will need to modify the script to account for these names.

    (2) the Date2Num functions in the calculation of the "totalDays" use the date format "YYYY-MM-DD". If your date habits differ from "YYYY-MM-DD" FormCalc will complain.

    Steve

  • get all the date of end of months between two dates

    Hi all, I'll try to find together the date of end of months between two dates.
    I have the following data in a table

    WITH AS's Data1
    (
    SELECT To_Date('4/30/2009','mm/dd/yyyy') dt, 'TEST' lname FROM dual UNION ALL
    SELECT To_Date('5/01/2009','mm/dd/yyyy') dt, "TEST2" lname FROM dual UNION all
    SELECT To_Date('5/02/2009','mm/dd/yyyy') dt, "TEST3" lname FROM dual UNION all
    SELECT To_Date('5/03/2009','mm/dd/yyyy') dt, "TEST4" lname FROM dual UNION all
    SELECT To_Date('5/04/2009','mm/dd/yyyy') dt, "TEST5" lname FROM dual UNION all
    SELECT To_Date('5/05/2009','mm/dd/yyyy') dt, "TEST6" lname FROM dual UNION ALL
    SELECT To_Date('5/31/2009','mm/dd/yyyy') dt, 'TEST7' lname FROM dual UNION all
    SELECT To_Date('6/01/2009','mm/dd/yyyy') dt, 'TEST' lname FROM dual

    )

    I would like to write an application that gives you all the end date for months between two dates
    for example, if you give the date 9/1/4 and 5/6/9, the output should be
    DT          LNAME
    -------------------
    4/30/2009   TEST
    IF I give you the dates 01/04/09 AND 01/06/09, THEN output should be
    DT          LNAME
    -------------------
    4/30/2009   TEST
    5/31/2009   TEST7
    as you can see, I want all the end dates of months between two dates by giving the output.

    If I give you 06/05/09 and 05/06/09, and then does so no output because there is no end of month between these two dates.
    Similarly, if I give you 30/04/09 and 30/04/09 then output should be 30/04/09 because it is an end of month date

    anyone can help to write a query. Thank you very much

    Jin
    Not sure if you need subquery, analytical functions for this.
    According to me, which should solve your purpose.

    WITH data1 AS
    (
    SELECT To_Date('4/30/2009','mm/dd/yyyy') dt, 'TEST' lname FROM dual UNION ALL
    SELECT To_Date('5/01/2009','mm/dd/yyyy') dt, 'TEST2' lname FROM dual UNION all
    SELECT To_Date('5/02/2009','mm/dd/yyyy') dt, 'TEST3' lname FROM dual UNION all
    SELECT To_Date('5/03/2009','mm/dd/yyyy') dt, 'TEST4' lname FROM dual UNION all
    SELECT To_Date('5/04/2009','mm/dd/yyyy') dt, 'TEST5' lname FROM dual UNION all
    SELECT To_Date('5/05/2009','mm/dd/yyyy') dt, 'TEST6' lname FROM dual UNION ALL
    SELECT To_Date('5/31/2009','mm/dd/yyyy') dt, 'TEST7' lname FROM dual UNION all
    SELECT To_Date('6/01/2009','mm/dd/yyyy') dt, 'TEST' lname FROM dual 
    
    )
    select distinct last_day(dt) dt,lname
      from data1
     where dt between &start_date and &end_date
       and last_day(dt) <= &end_date
    
    also, i have another request. how would i do this without using the data i provided. lets say,
     i tell you, give me alll the months end between two dates using dual in the from clause of a query.
    how would you do this in a query or pl/sql? thanks again
    

    Can be like that

    with data1 as(
    select &start_date+level-1 dt from dual
    connect by level <= &end_date-&start_date+1)
    select distinct last_day(dt)
      from data1
     where dt between &start_date and &end_date
       and last_day(dt) <= &end_date
     order by last_day(dt)
    

    See you soon!
    Bobin

    Published by: Buga added query for updated the need

  • Need to query to find missing months between two dates.

    A bank customer a monthly overview of databases (in the tens of millions of accounts)

    The data are sent with a line per account each month.  It is stored in a table called "ACCOUNTS_TAB".

    Fields on registration, which

    Column

    Description

    Example of format.

    * YRMTH-1

    Year and month of the snapshot

    6-character numeric, 4-digit year added to months of 2 digits, example 201301, 201311

    * AccountID

    Unique identifier for an account.

    Integer, example 100098322

    Opening balance - 1

    Initial account balance at the closing of the previous month

    $30000

    Closing balance - 1

    The account balance at the end of the month of closing

    $34200

    Class-1 relative to investments

    .....

    ...

    .....

    ......

    .....

    Many more columns, two favorites (*) columns are alone, that you will need for the query. There are no columns to tell when an account is closed, or has just opened, and not separate "master account" - you use this single table for your query that results.

    The table was in place from 200301, so many accounts have dozens of lines, if they have been open for many years and other only 1 or 2 ranks, if new.

    Write a query to give just a list of accountDs that meet the following criteria.

    1. 1) has a record for specific 201503
    2. 2) was also a record for the month specific 201602
    3. 3) missing one or several months between these two records.

    A good account which was opened from 201503 to 201602 inclusive would of course have lines for

    201503

    201504

    201505

    201506

    201507

    201508

    201509

    201510

    201511

    201512

    201601

    201602

    It's good to hardcode the month of departure (201503), end of month (201602) and the actual number of months between them (10 exclusive or 12 inclusive) as part of your query.

    Once again the account must meet all 3 criteria to be an account issue.  If only, they have a partial set of these documents but do not have the month of departure, or is not the end of the month, it is not a problem, only when they have so specified and end early and not a full between game.


    Help, please!

    Maybe this...

    -----

    -The Dataset for the test begins

    WITH dataset (ACCOUNT_NO, DATES)

    AS (SELECT 100, DOUBLE 201503

    UNION ALL

    SELECT 100, DOUBLE 201504

    UNION ALL

    SELECT 100, DOUBLE 201505

    UNION ALL

    SELECT 100, DOUBLE 201506

    UNION ALL

    SELECT 100, DOUBLE 201507

    UNION ALL

    SELECT 100, DOUBLE 201508

    UNION ALL

    SELECT 100, 201509 OF THE DOUBLE

    UNION ALL

    SELECT 100, DOUBLE 201510

    UNION ALL

    SELECT 100, 201511 OF THE DOUBLE

    UNION ALL

    SELECT 100, DOUBLE 201512

    UNION ALL

    SELECT 100, 201601 DOUBLE

    UNION ALL

    SELECT 100, DOUBLE 201602

    UNION ALL

    SELECT 200, DOUBLE 201503

    UNION ALL

    SELECT 200, DOUBLE 201504

    UNION ALL

    SELECT 200, DOUBLE 201505

    UNION ALL

    SELECT 200, DOUBLE 201506

    UNION ALL

    SELECT 200, DOUBLE 201508

    UNION ALL

    SELECT 200, DOUBLE 201509

    UNION ALL

    SELECT 200, DOUBLE 201510

    UNION ALL

    SELECT 200, DOUBLE 201511

    UNION ALL

    SELECT 200, DOUBLE 201512

    UNION ALL

    SELECT 200, 201601 DOUBLE

    UNION ALL

    SELECT 200, DOUBLE 201602

    UNION ALL

    SELECT 300, DOUBLE 201503

    UNION ALL

    SELECT 300, DOUBLE 201504

    UNION ALL

    SELECT 300, DOUBLE 201505

    UNION ALL

    SELECT 300, DOUBLE 201506

    UNION ALL

    SELECT 300, DOUBLE 201507

    UNION ALL

    SELECT 300, DOUBLE 201508

    UNION ALL

    SELECT 300, DOUBLE 201509

    UNION ALL

    SELECT 300, DOUBLE 201510

    UNION ALL

    SELECT 300, DOUBLE 201511

    UNION ALL

    SELECT 300, 201601 DOUBLE

    UNION ALL

    SELECT 300, DOUBLE 201602

    UNION ALL

    SELECT 400, DOUBLE 201504

    UNION ALL

    SELECT 400, DOUBLE 201505

    UNION ALL

    SELECT 400, DOUBLE 201506

    UNION ALL

    SELECT 400, DOUBLE 201507

    UNION ALL

    SELECT 400, 201508 DOUBLE),

    -The Dataset to test ends

    -Hand with clause begins

    ResultSet

    AS (SELECT account_no,

    CASE

    WHEN COUNT)

    CASE WHEN dates IN (201503, 201602) THEN 1 END)

    COURSES (PARTITION BY account_no) = 2

    AND (COUNT (DISTINCT SUBSTR (dates, 4)))

    COURSES (PARTITION BY account_no) = 12

    OR COUNT (DISTINCT SUBSTR (dates, 4))

    COURSES (PARTITION BY account_no)<>

    AND COUNT (DISTINCT SUBSTR (dates, 1, 4))

    (PARTITION BY account_no) > 1

    THEN

    « Y »

    ON THE OTHER

    « N »

    END

    FLG

    THE dataset---> replace with your table name

    GROUP BY account_no, dates)

    SELECT DISTINCT Account_no

    FROM THE result set

    WHERE flg = 'Y ';

    -Hand with the ends of the clause

    ACCOUNT_NO

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

    100

    300

    200

    See you soon,.

    Manik.

  • How to get the days and months when two dates are given

    Hi all

    I have a requirement where I need to return the number of months and days between two given dates.
    I have no need to take account of the year as the difference in dates is always less than a year

    I have 28 June 2012 and January 31, 2013, as dates.

    I'm working on 10g

    When I select the double months_between(:a,:b) and provide the dates he gives me 7.09677
    I want to give only 7 months and 9 days, or what ever the exact days are.

    Your contributions are appreciated

    Try this:

    WITH t AS
    (SELECT TO_DATE('28-JUN-2012', 'DD-MON-YYYY') date1, TO_DATE('31-JAN-2013', 'DD-MON-YYYY') date2 FROM dual)
    SELECT TRUNC(MONTHS_BETWEEN(date2, date1)) months,
           date2 - ADD_MONTHS(date1, TRUNC(MONTHS_BETWEEN(date2, date1))) days
    FROM   t
    
  • Need help on the list of months between two Dates

    Hi all

    I have a table which has startdate and enddate and need a select to list all the months between these two dates for each given ID.
    I did some tests and could not figure out how to get the startdate and enddate table (instead of hard coding) tests in the select statement.

    Could someone please help on this (Oracle 11 g 2),
    Thanks in advance!


    create table testing(
    id          number,
    start_date  date,
    end_date    date);
    insert into testing values(100, to_date('05-FEB-2011', 'DD-MON-YYYY'), to_date('28-MAY-2011', 'DD-MON-YYYY'));
    insert into testing values(200, to_date('20-JUN-2011', 'DD-MON-YYYY'), to_date('28-DEC-2011', 'DD-MON-YYYY'));
    commit;
    
    select * from testing;
    
            ID START_DAT END_DATE
    ---------- --------- ---------
           100 05-FEB-11 28-MAY-11
           200 20-JUN-11 28-DEC-11
    
    Elapsed: 00:00:00.01
    
    *for testing.id = 100:*
    select to_char(add_months(to_date('05-FEB-2011', 'DD-MON-YYYY'), l - 1), 'YYYY-Mon') Dates
     from (select level l
           from dual
        connect by level <= months_between(trunc(to_date('28-MAY-2011', 'DD-MON-YYYY'), 'MONTH'),
                                           trunc(to_date('05-FEB-2011', 'DD-MON-YYYY'), 'MONTH')) + 1);
    
    DATES
    -----------------
    2011-Feb
    2011-Mar
    2011-Apr
    2011-May
    
    Elapsed: 00:00:00.01
    
    *for testing.id = 200:*
    select to_char(add_months(to_date('20-JUN-2011', 'DD-MON-YYYY'), l - 1), 'YYYY-Mon') Dates
     from (select level l
           from dual
        connect by level <= months_between(trunc(to_date('28-DEC-2011', 'DD-MON-YYYY'), 'MONTH'),
                                           trunc(to_date('20-JUN-2011', 'DD-MON-YYYY'), 'MONTH')) + 1);
    
    DATES
    -----------------
    2011-Jun
    2011-Jul
    2011-Aug
    2011-Sep
    2011-Oct
    2011-Nov
    2011-Dec
    
    7 rows selected.
    SQL> select * from testing
      2  /
    
            ID START_DAT END_DATE
    ---------- --------- ---------
           100 05-FEB-11 28-MAY-11
           200 20-JUN-11 28-DEC-11
    
    SQL> select  id,
      2          to_char(add_months(start_date,column_value - 1),'YYYY-Mon') dates
      3    from  testing,
      4          table(
      5                cast(
      6                     multiset(
      7                              select  level
      8                                from  dual
      9                                connect by add_months(trunc(start_date,'MM'),level - 1) <= end_date
     10                             )
     11                     as sys.OdciNumberList
     12                    )
     13               )
     14    order by id,
     15             column_value
     16  /
    
            ID DATES
    ---------- --------
           100 2011-Feb
           100 2011-Mar
           100 2011-Apr
           100 2011-May
           200 2011-Jun
           200 2011-Jul
           200 2011-Aug
           200 2011-Sep
           200 2011-Oct
           200 2011-Nov
           200 2011-Dec
    
    11 rows selected.
    
    SQL> 
    

    SY.

  • Gets the number of months to a Date

    Hello

    I use the version of database Oracle 11.2.1. I would like to calculate the number of fiscal month of a date.

    For example:

    Date: 31/03/2013
    Fiscal No. month: 12

    The fiscal year runs from 01 - Apr-31 - March each year.

    Hello

    You can use

    EXTRACT (MONTH FROM (ADD_MONTHS (mydate, -3)))
    

    Example:

    WITH mytable AS (    SELECT TRUNC(SYSDATE) - 400 + LEVEL mydate
                           FROM DUAL
                     CONNECT BY LEVEL <= 400)
    SELECT mydate, EXTRACT (MONTH FROM (ADD_MONTHS (mydate, -3))) fiscal_month
      FROM mytable
     WHERE mydate = TRUNC (mydate, 'MM')
        OR mydate = LAST_DAY(mydate);
    
    MYDATE    FISCAL_MONTH
    --------- ------------
    31-MAR-12           12
    01-APR-12            1
    30-APR-12            1
    01-MAY-12            2
    31-MAY-12            2
    01-JUN-12            3
    30-JUN-12            3
    01-JUL-12            4
    31-JUL-12            4
    01-AUG-12            5
    31-AUG-12            5
    01-SEP-12            6
    30-SEP-12            6
    01-OCT-12            7
    31-OCT-12            7
    01-NOV-12            8
    30-NOV-12            8
    01-DEC-12            9
    31-DEC-12            9
    01-JAN-13           10
    31-JAN-13           10
    01-FEB-13           11
    28-FEB-13           11
    01-MAR-13           12
    31-MAR-13           12
    01-APR-13            1
    

    Kind regards.
    Al

  • Number of days between two Dates in the Correct month

    I am trying to write a SELECT statement that returns the number of days spent on a project.

    Here's my problem. I have a table with several columns of date (start_date, end_date, etc.) as well as several other columns. Here is an example of the table.

    PROJECT... START_DATE... END_DATE
    123 ................. 1ST JANUARY 13... 15 JANUARY 13
    456 ................. 25 JANUARY 13... FEBRUARY 5, 13
    789 ................. 30 JANUARY 13... FEBRUARY 5, 13
    999 ................. 1ST FEBRUARY 13... FEBRUARY 8, 13

    I'm counting the number of days spent on projects in each month. For example, with the above data, I would come back...

    MONTHS... PROJECT_DAYS
    Jan ............. 24
    Feb ............. 18

    I tried to use a CASE statement, but I'm having a hard time to understand. Any help would be greatly appreciated!

    Published by: 987079 on February 8, 2013 13:12

    An option would be something like

      1  with project as (
      2    select 123 project_id, date '2013-01-01' start_date, date '2013-01-15' end_date from dual union all
      3    select 456, date '2013-01-25', date '2013-02-05' from dual union all
      4    select 789, date '2013-01-30', date '2013-02-05' from dual union all
      5    select 999, date '2013-02-01', date '2013-02-08' from dual
      6  ),
      7  all_days as (
      8    select start_date + level - 1 dt
      9      from (select min(start_date) start_date,
     10                   max(end_date) end_date
     11              from project)
     12   connect by level <= end_date - start_date + 1
     13  )
     14  select trunc(dt,'MM'),
     15         count(*)
     16    from all_days ad
     17         join project p on (ad.dt between p.start_date and p.end_date)
     18   group by trunc(dt,'MM')
     19*  order by trunc(dt,'MM')
    SQL> /
    
    TRUNC(DT,'MM')        COUNT(*)
    ------------------- ----------
    2013-01-01 00:00:00         24
    2013-02-01 00:00:00         18
    

    Justin

Maybe you are looking for