Hours between two dates

Hi friends,

I use the Oracle 10 g server.

Can someone help me find hours of day remained wise room_no.

create the table ROOM_OCCUPIED

(

PERSON_NAME VARCHAR2 (10),

ROOM_NO VARCHAR2 (5).

DATE OF CHECK_IN,

DATE OF CHECK_OUT

)

insert into ROOM_OCCUPIED (ROOM_NO, CHECK_IN, PERSON_NAME, CHECK_OUT)

values ('JB', ' 20', to_date (March 5, 2015 19:00 ',' dd-mm-yyyy hh24:mi:ss'), to_date (May 5, 2015 12:00 ',' dd-mm-yyyy hh24:mi:ss'));))

insert into ROOM_OCCUPIED (ROOM_NO, CHECK_IN, PERSON_NAME, CHECK_OUT)

values ('CHRISTOPER', ' 15', to_date (January 5, 2015 09:00 ',' dd-mm-yyyy hh24:mi:ss'), to_date (5 March 2015 10:00 ',' dd-mm-yyyy hh24:mi:ss'));))

insert into ROOM_OCCUPIED (ROOM_NO, CHECK_IN, PERSON_NAME, CHECK_OUT)

values ('BIBI', '35', to_date (May 5, 2015 13:10 ',' dd-mm-yyyy hh24:mi:ss'), null);

insert into ROOM_OCCUPIED (ROOM_NO, CHECK_IN, PERSON_NAME, CHECK_OUT)

values ('DEV', ' 20', to_date (5 March 2015 01:00 ',' dd-mm-yyyy hh24:mi:ss'), to_date (5 March 2015 05:00 ',' dd-mm-yyyy hh24:mi:ss'));))

Select t.*, (Nvl (t.check_out, Sysdate) - t.check_in) * 24 Hrs

of room_occupied t

ORDER BY 2

PERSON_NAME ROOM_NO CHECK_IN CHECK_OUT TOT_HRS_STAYED

CHRISTOPER 15 5/1/2015 09:00 03/05/2015 10:00 49

20 H 5/3/2015 19:00 05/05/2015 12:00 41

DEV 20 5/3/2015 01:00 03/05/2015 05:00 4

BIBI 35 5/5/2015 13:10 10.66888889

I need output like below a day format

Name of person room occupied_dt Hrs_stayed

Christoper 15 24 05/01/2015

Christoper 15 05/02/2015 24 hours

Christoper 15 9hrs 05/03/2015

- - - - - - - - -

- - - - - - - - -

- - - - - - - - -

I run every day to Watch report

Rgds,

RDK

Hi Rdk,

Here is my path. I'm counting minutes and then round up to hours for a day of occupation. To get the hours for those who are not yet extracted, I used check_out nvl (checked_out, sysdate). If not wanted then just delete expression nvl .

alter session set nls_date_format='dd-mm-yyyy'
;
with occupation_minutes (person_name, room_no, check_in, check_out, occupied_dt, minutes) as (
  select
    person_name,
    room_no,
    check_in,
    nvl(check_out, sysdate) check_out,
    trunc(check_in) occupied_dt,
    1 minutes
  from room_occupied
  union all
  select
    person_name,
    room_no,
    check_in,
    check_out,
    trunc(check_in + minutes / 1440),
    minutes + 1
  from occupation_minutes
  where minutes < (check_out - check_in) * 1440
)
select
  person_name,
  room_no,
  occupied_dt,
  round(count(minutes) / 60) || 'hrs' Hrs_stayed
from occupation_minutes
group by
  person_name,
  room_no,
  occupied_dt
order by
  person_name,
  occupied_dt
;

session SET altered.
PERSON_NAME ROOM_NO OCCUPIED_DT HRS_STAYED
----------- ------- ----------- -------------------------------------------
BIBI        35      05-05-2015  11hrs
BIBI        35      06-05-2015  8hrs
CHRISTOPER  15      01-05-2015  15hrs
CHRISTOPER  15      02-05-2015  24hrs
CHRISTOPER  15      03-05-2015  10hrs
DEV         20      03-05-2015  4hrs
SIDHIQ      20      03-05-2015  5hrs
SIDHIQ      20      04-05-2015  24hrs
SIDHIQ      20      05-05-2015  12hrs                                     

9 rows selected

Tags: Database

Similar Questions

  • Calculate business hours between two dates in a cursor

    Hello, its me again (?).

    Yesterday, I got a solution to a problem here where I need to calculate the hours between two dates, excluding Sat/Sun and from 09:00 to 18:00. I'm now putting this login in a PL/SQL block, so I can calculate it for each record in a slider, all by looping.
    with t (start_date
           ,end_date
           )
    as (select  to_date('22-oct-2012 09:00:00','dd-mon-yyyy hh24:mi:ss') start_dt,
                           to_date('23-oct-2012 09:00:00','dd-mon-yyyy hh24:mi:ss') end_dt
        from dual
       )
    , hrs (dt) as
      (select start_date
       from   t
       union all
       select dt +1/24/60
       from   hrs
       where  hrs.dt < (select end_date-1/24/60 from t)
      )
    select count(*)/60
    from hrs
    where to_char(dt,'dy') not in ('sat','sun')
    and  to_number(to_char(dt,'hh24')) not between 18 and 23
    and  to_number(to_char(dt,'hh24')) not between 0 and 8
    /
    The logic above, I'm up here:
    FOR c_record IN c_1 LOOP
    //For each record, check if the two dates meet the criteria and then raise the counters accordingly
    select  to_date(c_record.REPORTADA,'dd-mon-yyyy hh24:mi:ss') INTO vFch1 FROM DUAL;
    SELECT  to_date(c_record.INICIOREAL,'dd-mon-yyyy hh24:mi:ss') INTO vFch2 FROM DUAL;
    
    IF ((vHoras > 2) AND c_record.PRIORIDAD=3) THEN
      vContOk :=vContOk + 0;
      vContBad := vContBad+1;
      vContOt   := vContOt+1;
    ELSE 
      vContOk :=vContOk +1;
      vContBad := vContBad+0;
      vContOt   := vContOt+1;
    END
        IF;
    
    IF ((vHoras > 4) AND c_record.PRIORIDAD=2) THEN
      vContOk :=vContOk + 0;
      vContBad := vContBad+1;
      vContOt   := vContOt+1;
    ELSE
      vContOk :=vContOk +1;
      vContBad := vContBad+0;
      vContOt   := vContOt+1;
    END IF;
    
    IF ((vHoras > 16) AND c_record.PRIORIDAD=1) THEN
      vContOk :=vContOk + 0;
      vContBad := vContBad+1;
      vContOt   := vContOt+1;
    ELSE
      vContOk :=vContOk +1;
      vContBad := vContBad+0;
      vContOt   := vContOt+1;
    END IF;
    
    END LOOP;
    I really don't like if the performance is horrible because it will run once a month and should spend only 40-100 records.
    However, I can't find a way to use this connection even in this case

    Could you help me?
    Thank you!

    Greetings,
    NACEUR

    N wrote:
    I need to calculate the number of minutes between two dates, only count the hours from 9 to 18hs.

    CREATE OR REPLACE
      FUNCTION get_bus_minutes_between(
                                       p_start_date DATE,
                                       p_end_date DATE
                                      )
        RETURN NUMBER
        IS
            v_return NUMBER;
        BEGIN
            with t as (
                       select  case level
                                 when 1 then greatest(p_start_date,trunc(p_start_date) + 9 / 24)
                                 else trunc(p_start_date) + level - 15 / 24
                               end start_dt,
                               case connect_by_isleaf
                                 when 1 then least(p_end_date,trunc(p_end_date) + 18 / 24)
                                 else trunc(p_start_date) + level - 8 / 24
                               end end_dt
                         from  dual
                         connect by level <= trunc(p_end_date) - trunc(p_start_date) + 1
                      )
            select  sum(greatest(end_dt - start_dt,0)) * 24 * 60 work_minutes
              into  v_return
              from  t
              where trunc(start_dt) - trunc(start_dt,'iw') < 5; -- exclude weekends
            RETURN v_return;
    END;
    /
    

    For example:

    SQL> select get_bus_minutes_between(
      2                                 to_date('20-oct-2012 13:00:00','dd-mon-yyyy hh24:mi:ss'),
      3                                 to_date('22-oct-2012 13:21:00','dd-mon-yyyy hh24:mi:ss')
      4                                ) work_minutes
      5    from dual
      6  /
    
    WORK_MINUTES
    ------------
             261
    
    SQL> 
    

    SY.

  • Calculate the hours between two dates

    Hello

    I have a requirement to count the hours with 2 given timestamps, something like:
    SQL> select ((to_date('22-oct-2012 13:00:00','dd-mon-yyyy hh24:mi:ss') - to_date('20-oct-2012 13:00:00','dd-mon-yyyy hh24:mi:ss'))*24) B from dual ;
    
             B
    ----------
            48
    However, I wish it would be that simple. I just need to count the hours between two timestamps for the hours of work for example, 08:00 - 17:00, MONDAY to Friday (excluding weekends).
    So lets say I have to count the hours between two dates above the result should be 18 hours not 48 (4 hours on 20/oct, 9 hours on 21/oct to 5 hours 22/oct).
    How can I achieve this? Any idea will be useful.

    Thank you

    My query generates a list of all the hours between the start and end time, filters on the hours that are outside of the hours of work and then account for the remaining lines. Change that to partial hours must just be a case of replace the two occurrences of 1/24 with 24/01/60, then - as say you - Division COUNT (*) by 60.

    There may be performance issues using techniques of generation of line like this, if you put this in a query involving the joining of several lines of other tables (for example where the dates of beginning and end are derived from several employees?) or in several days. If you don't find it then count the number of whole days (taking weekends into account) between dates and work on the partial days after that it could be better. This would probably involve a lot of statements of case (which I don't have time to test it now).

    Ben

  • How to calculate the hours between two dates by the numbers

    If I update 09:00 start time 17:00 end time of shift in C3 and B3, how to use a D3 formula to calculate the number of hours between the two?  Then I can just copy down the lines for nth years...

    It is a spreadsheet of the part-time staff payroll.

    I'm sure that a lot of people out there have done that.

    Help, please.

    Thank you.

    Eddie

    What program of worksheet that you are using? Since you have a newer iMac running El Capitan, I can safely assume that you use NOT AppleWorks, which is a Power PC application that can run on any OS newer than the snow leopard, OS X 10.6.8.

    If you use numbers, try to repost your question in numbers for Mac forum. If you use Microsoft Excel, try posting in the Microsoft forums. LibreOffice also has community support.

  • Excerpt from the hours between two dates

    Hello

    This is the value stored in the table of my database.

    I want to extract betwwen hours two dates. How can I extract it.

    I try to run like this

    Select to_timestamp('27-JAN-14 05.30.00.000000 PM')-to_timestamp('26-JAN-13 08.20.00.000000 PM') double, but it gives the answer as the format 21:10:00.000000000 + 000000365

    I want simple hours, minutes, and seconds

    What is the format of this value as "dd-MON-yy."

    27 JANUARY 14 01.30.00.000000 PM

    26 JANUARY 14 06.22.32.170033 PM

    Use the Extract function. Like this.

    () AS T1 (C1)

    Select to_timestamp('27-JAN-14 05.30.00.000000 PM')-to_timestamp('26-JAN-13 08.20.00.000000 PM') of double)

    SELECT EXTRACT (DAY OF C1) | ' DAYS '.

    EXTRACT (HOUR OF C1) | ' HOURS '.

    EXTRACT (MINUTE OF C1) | ' MINUTES '.

    EXTRACT (SECOND C1) | ' SECONDS

    FROM T1;

    OUTPUT:

    365 DAYS 21 HOURS 10 MINUTES 0 SECONDS

    If not try it.

    SELECT EXTRACT (DAY OF to_timestamp('27-JAN-14 05.30.00.000000 PM')-to_timestamp('26-JAN-13 08.20.00.000000 PM')) | ' DAYS '.

    EXTRACT (HOUR FROM to_timestamp('27-JAN-14 05.30.00.000000 PM')-to_timestamp('26-JAN-13 08.20.00.000000 PM')) | ' HOURS '.

    EXTRACT (to_timestamp('27-JAN-14 05.30.00.000000 PM')-to_timestamp('26-JAN-13 08.20.00.000000 PM') MINUTE) | ' MINUTES '.

    EXCERPT ((SECOND from to_timestamp('27-JAN-14 05.30.00.000000 PM')-to_timestamp('26-JAN-13 08.20.00.000000 PM')) |) ' SECONDS

    DOUBLE;

    Post edited by: Parth272025

  • Calculate business hours between 2 dates with negative numbers

    Hi all

    I use a function to calculate the difference between 2 dates schedules. The function was taken and adapted from this thread:

    Calculate business hours between two dates in a cursor

    CREATE OR REPLACE
      FUNCTION get_bus_minutes_between(
                                       p_start_date DATE,
                                       p_end_date DATE
                                      )
        RETURN NUMBER
        IS
            v_return NUMBER;
        BEGIN
            with t as (
                       select  case level
                                 when 1 then greatest(p_start_date,trunc(p_start_date) + 9 / 24)
                                 else trunc(p_start_date) + level - 15 / 24
                               end start_dt,
                               case connect_by_isleaf
                                 when 1 then least(p_end_date,trunc(p_end_date) + 20 / 24)
                                 else trunc(p_start_date) + level - 4 / 24
                               end end_dt
                         from  dual
                         connect by level <= trunc(p_end_date) - trunc(p_start_date) + 1
                      )
            select  sum(greatest(end_dt - start_dt,0)) * 24 * 60 work_minutes
              into  v_return
              from  t
              where trunc(start_dt) - trunc(start_dt,'iw') < 5; -- exclude weekends
            RETURN v_return;
        END;
        /

    I am running into an issue that when the p_end_date is before the p_start_date value the function returns the full time between the dates without regard for business hours. if the end_date is before the start date I get the expected result i.e. difference between the dates in business hours.

    Using example dates of start 19th October 2012 13:00:00 and end 22nd october 2012 13:21:00 the business minutes are 681. However if i reverse the dates I get -4341.


    Correct labour code is the following, I've annotated so that he could help someone else looking for a solution like this:

    CREATE OR REPLACE
      FUNCTION get_bus_minutes_between(
                                       p_start_date DATE,  --these are the 2 dates passed into the function
                                       p_end_date DATE     --these are the 2 dates passed into the function
    )
    RETURN NUMBER
    IS
    v_return NUMBER;

            l_start  DATE;

            l_end    DATE;

            l_factor NUMBER;
        BEGIN

            IF p_start_date <= p_end_date THEN            --this IF section checks which of the 2 dates is the greatest

               l_start  := p_start_date;                  -- and then assigns the earliest date to p_start_date

               l_end    := p_end_date;

               l_factor := 1;

            ELSE   -- swap the dates around, and multiply the result by -1

               l_start  := p_end_date;

               l_end    := p_start_date;

               l_factor := -1;

            END IF;

            with t as (
                       select  case level
                                 when 1 then greatest(l_start,trunc(l_start) + 9 / 24)  -- this sets the start of working hours
                                 else trunc(l_start) + level - 15 / 24  --if the start time is adjusted this value must also be adjusted
                               end start_dt,
                               case connect_by_isleaf
                                 when 1 then least(l_end,trunc(l_end) + 20 / 24)  -- this sets the end of working hours
                                 else trunc(l_start) + level - 4 / 24  -- if the end time is adjusted this value must also be adjusted
                               end end_dt
                         from  dual
                         connect by level <= trunc(l_end) - trunc(l_start) + 1
                      )
            select  sum(end_dt - start_dt) * 24 * 60 work_minutes  -- this subtracts the 2 dates and then multiplies by hours and minutes
              into  v_return
              from  t
              where trunc(start_dt) - trunc(start_dt,'iw') < 5; -- this exclude weekends
            RETURN v_return * l_factor;  -- this gives the negative or positive results so you can see if it was completed before or after the required date.
        END;
        /

  • Count the number of hours between the dates off holiday/weekend

    Hi all

    I've not worked with dates and was recently asked to create a report where I am looking for the number of hours between two dates only count business days.

    So for example I have given as follows

    Created on 2011-03-30 15:00
    Treaty of 2011-03-30 15:03:46
    Filled 2011-04-01 17:25:02
    Posted 2011-04-01 17:45

    For a total of looking for the CREATION and dates

    50 hours and 45 minutes

    IM also trying to exclude weekends and holidays, I read around and was able to also find an array of dates where I main date, the HOLIDAY_IND column, and the column WEEKDAY_IND

    If the calendar table I have looks like

    CALENDER_DATE HOLIDAY_IND WEEKDAY_IND
    2011-03-31 YY
    2011-04-01 N Y
    2011-04-02 N N

    IM really very puzzled to know where to start

    I thought I'd try to write with PL/SQL, but I don't have access of the user to create procedures/functions, so looks like right up to SQL

    Any help is appreciated!

    Hello

    Depeneding on your data and your needs, you can do something like this:

    SELECT     created_DATE,
    ,     mailed_date
    ,     24 * ( (mailed_date - created_date)
              - (
                SELECT  COUNT (*)
                FROM        table_o_dates
                WHERE    main_date > created_date
                AND        main_date < TRUNC (mailed_date)
                AND        (   holiday_ind = 'Y'
                         OR  weekday_ind = 'N'
                      )
              )
               )               AS hours_between
    FROM     table_x
    ;
    

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

    What do you do if created_date or mailed_date is not a working day? Examples in you data and results.

  • County of days between two dates without weekend

    Hello
    I need a solution in the query or another thread, which returns the number of days between two dates without considering the weekend (Saturday and Sunday), I have a column of type Date, and return need in the form of a column HH hours and days in another column.

    Concerning
    Jonas

    Hi and welcome to the forum.

    Don't forget you can do a search on this forum.
    Your question has been asked before.

    Some other tips:

    http://asktom.Oracle.com/pls/asktom/asktom.download_file?p_file=6551242712657900129
    http://asktom.Oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:185012348071

  • photo selection between two dates

    In Photos

    It is possible to select photos between two dates?

    In Photos,.

    is it possible to select the photos between two dates?

    Thank you, thank you

    Yes.  With a smart album like this:

  • MaxL Script to clear the data between two dates

    Hi all

    I need advice to clear the data between two dates, I have three dimensions in my sketch, 'eno', 'hiredate' and the 'actualamount '.

    Now I need to erase the data between the date range, up to now, I have this script,

    Fix ("HireDate", "Eno")

    Difficulty (@relative ("00:00:00",0),@relative("eno",0)) 2015-07-15 "))

    CLEARDATA "sal."

    endfix

    ENDFIX

    These scripts only clears on a specific day, but I tried to write the script to clear between two dates, I surfed on a few sites, but no clear answers, finaanly came here, kindly help in this regard.

    Thanks in advance.

    Have you not tried the format of "startdate":"enddate" for example "August":"September."

    See you soon

    John

  • 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

  • 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)'))

  • 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>
    
  • Days between two dates given

    Hi all

    I need the days between two dates given.

    Select to_date('25-mar-2014','dd-mon-yyyy') start_date, end_date to_date('30-mar-2014','dd-mon-yyyy')

    of the double

    Desired output:

    25

    26

    27

    28

    29

    30

    Thanks in advance.

    Kind regards

    Afzal.

    Select the level - 1 desired_output + to_number (to_char (start_date, 'dd'))

    of (to_date('25-mar-2014','dd-mon-yyyy') select start_date, end_date to_date('30-mar-2014','dd-mon-yyyy'))

    of the double

    )

    connect by level<=  end_date="" -="" start_date="" +="">

    DESIRED_OUTPUT
    25
    26
    27
    28
    29
    30

    Concerning

    Etbin

  • 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

Maybe you are looking for