Number of hours of the weekend between two timestamps

I'm trying to take two timestamps and the total number of hours weekend between them.  For example:

Between October 23, 13 08.28.00.000000 AM and 28 October 13 04.48.25.424940 PM, there are two weekend days.  I want the query to return the 48 hours.

Between October 24, 13 04.48.25.424940 PM and 01.00.00.000000 October 27, 13 h ago weekend day plus 13 hours Sunday, I would that this return to 37 hours.

Database version:

Oracle Database 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production

PL/SQL Release 11.2.0.3.0 - Production

"CORE 11.2.0.3.0 Production."

AMT for Linux: Version 11.2.0.3.0 - Production

NLSRTL Version 11.2.0.3.0 - Production

Create / Insert statements:

CREATE TABLE 'TDIFF.

(NUMBER OF 'ID',

"TS1' TIMESTAMP (6).

('TS2' TIMESTAMP (6));

Insert into TDIFF (ID, TS1 and TS2) values (1, to_timestamp (17 October 13 02.09.01.818304000 PM ',' DD-MON-RR HH.MI.)) PSM AM FF'), to_timestamp (30 October 13 09.16.00.000000000 PM ',' DD-MON-RR HH.MI.) PSM AM FF '));

Insert into TDIFF (ID, TS1 and TS2) values (2, to_timestamp (21 October 13 01.23.58.258494000 PM ',' DD-MON-RR HH.MI.)) PSM AM FF'), to_timestamp (24 October 13 10.00.00.000000000 AM ',' DD-MON-RR HH.MI.) PSM AM FF '));

Insert into TDIFF (ID, TS1 and TS2) values (3, to_timestamp (07.33.10.874721000 AM October 23, 13 ',' DD-MON-RR HH.MI.)) PSM AM FF'), to_timestamp (24 October 13 10.00.00.000000000 AM ',' DD-MON-RR HH.MI.) PSM AM FF '));

Insert into TDIFF (ID, TS1 and TS2) values (4, to_timestamp (09.12.41.928313000 AM October 23, 13 ',' DD-MON-RR HH.MI.)) PSM AM FF'), to_timestamp (25 October 13 02.30.00.000000000 PM ',' DD-MON-RR HH.MI.) PSM AM FF '));

Insert into TDIFF (ID, TS1 and TS2) values (5, to_timestamp (10.12.22.790736000 AM October 23, 13 ',' DD-MON-RR HH.MI.)) PSM AM FF'), to_timestamp (24 October 13 06.40.00.000000000 PM ',' DD-MON-RR HH.MI.) PSM AM FF '));

Insert into TDIFF (ID, TS1 and TS2) values (6, to_timestamp (10.33.20.879307000 AM October 23, 13 ',' DD-MON-RR HH.MI.)) PSM AM FF'), to_timestamp (25 October 13 04.24.00.000000000 PM ',' DD-MON-RR HH.MI.) PSM AM FF '));

Insert into TDIFF (ID, TS1 and TS2) values (7, to_timestamp (11.01.23.467642000 AM October 23, 13 ',' DD-MON-RR HH.MI.)) PSM AM FF'), to_timestamp (24 October 13 07.00.00.000000000 AM ',' DD-MON-RR HH.MI.) PSM AM FF '));

Insert into TDIFF (ID, TS1 and TS2) values (8, to_timestamp (23 October 13 04.45.08.237177000 PM ',' DD-MON-RR HH.MI.)) PSM AM FF'), to_timestamp (07.00.00.000000000 AM October 25, 13 ',' DD-MON-RR HH.MI.) PSM AM FF '));

Insert into TDIFF (ID, TS1 and TS2) values (9, to_timestamp (23 October 13 04.48.25.424940000 PM ',' DD-MON-RR HH.MI.)) PSM AM FF'), to_timestamp (28 October 13 08.28.00.000000000 AM ',' DD-MON-RR HH.MI.) PSM AM FF '));

This is what I have so far, but his return the number of incorrect hours:

SELECT TS1,TS2,
  (SELECT COUNT(*)
   FROM DUAL
   WHERE TO_CHAR(TS2 + LEVEL - interval '1' hour, 'DY') IN ('SAT', 'SUN') 
CONNECT BY LEVEL <= 24*extract(DAY FROM TS2-TS1)+extract(hour FROM TS2-TS1) + 1 ) D1_WEEKEND_HOURS
FROM TDIFF

Any ideas are appreciated.

-Steve

Here is another way, assuming a definition of the weekend as strictly between midnight Friday and midnight Sunday:

The interval of D1 to D2 can be considered be allowed between the D1 and the next 00 Monday more time between Monday preceding the D2 and D2 as well as the weeks between the two Monday to 00:00.

select  id, ts1, ts2,
( least(7 + (trunc(CAST(ts1 as DATE), 'IW') - CAST(ts1 as DATE)), 2) /* ts1 to Monday */
+ greatest( (CAST(ts2 as DATE) - trunc(CAST(ts2 as DATE), 'IW')) -5, 0) /* Monday to ts2 */
+ 2/7 * (trunc(CAST(ts2 as DATE), 'IW') - trunc(CAST(ts1 as DATE), 'IW') -7 ) /* Weeks between Mondays */
) * 24
from TDIFF

Tags: Database

Similar Questions

  • How to store the difference between two timestamps as a number?

    Hello
    I need to find the difference between 2 variables of timestamp and the result will be stored as a number in minutes?
    as
    declare
    timestamp (7) of the fir_timestamp: = 1 January 2012 13:30 ';
    timestamp (7) of the sec_timestamp: = 1 January 2012 14:00 ';
    c number;
    Start
    c:=((sec_timestamp-fir_timestamp)/(24*60));
    dbms_output.put_line (c);
    end;

    962813 wrote:
    declare

    -fir_timestamp timestamp (7): ='01 - jan - 2012 13:30 ';
    timestamp (7) of the sec_timestamp: = 1 January 2012 14:00 ';  Incorrect type declaration

    fir_timestamp timestamp: = 1st January 12 01.30.00.000000 PM';
    sec_timestamp timestamp: = 1st January 12 02.00.00.000000 PM';
    c number;
    Start
    c: = ABS ((extrait (HEURE de sec_timestamp) - extract (fir_timestamp TIME)) * 60) +.
    ABS ((extrait (MINUTE de sec_timestamp) - extract (fir_timestamp MINUTE)));
    dbms_output.put_line (' interval Min Total:-' | c);
    end;
    /
    declare
    *
    ERROR on line 1:
    ORA-01830: date format picture ends before converting all of the input string
    ORA-06512: at line 6

    Follow what Paul has done...

    In fact, you do not need timestamp, date data type is enough

    SQL> declare
      2     fir_timestamp date :=
      3        to_date('01-jan-2012 13:30:00','dd-mon-yyyy hh24:mi:ss');
      4     sec_timestamp date :=
      5        to_date('01-jan-2012 14:00:00','dd-mon-yyyy hh24:mi:ss');
      6     c number;
      7  begin
      8     c:= round((sec_timestamp-fir_timestamp)*24*60);
      9     dbms_output.put_line(c);
     10  end;
     11  /
    30
    
    PL/SQL procedure successfully completed.
    

    If you want to place on timestamp

    SQL> declare
      2     fir_timestamp timestamp :=
      3        to_timestamp('01-jan-2012 13:30:00','dd-mon-yyyy hh24:mi:ss');
      4     sec_timestamp timestamp :=
      5        to_timestamp('01-jan-2012 14:00:00','dd-mon-yyyy hh24:mi:ss');
      6     c number;
      7  begin
      8     c:= round((
      9     to_date(to_char(sec_timestamp,'ddmmyyyyhh24miss'),
     10             'ddmmyyyyhh24miss')-
     11          to_date(to_char(fir_timestamp,'ddmmyyyyhh24miss'),
     12             'ddmmyyyyhh24miss')
     13           )*24*60);
     14     dbms_output.put_line(c);
     15  end;
     16  /
    30
    
  • Type of data resulting from the difference between two timestamps

    Hi, I have a simple question.
    I have two TIMESTAMP A and B variables. Now I want to subtract one B - A. What type of data as a result of this subtraction?
    Would Shouln t TIMESTAMP again?

    Consider this example:
    DECLARE
       vdat_Begin        TIMESTAMP;
       vdat_End          TIMESTAMP;
       vdat_Difference   TIMESTAMP;
    BEGIN
       vdat_Difference := vdat_End - vdat_Begin;   -- this doesn't work
       vdat_Difference := TO_TIMESTAMP(vdat_End - vdat_Begin);   -- this works
    END;
    /
    
    PLS-00382: expression is of wrong type
    Thanks for help.

    xxsawer wrote:
    Hi, I have a simple question.
    I have two TIMESTAMP A and B variables. Now I want to subtract one B - A. What type of data as a result of this subtraction?
    Would Shouln t TIMESTAMP again?

    Consider this example:

    DECLARE
    vdat_Begin        TIMESTAMP;
    vdat_End          TIMESTAMP;
    vdat_Difference   TIMESTAMP;
    BEGIN
    vdat_Difference := vdat_End - vdat_Begin;   -- this doesn't work
    vdat_Difference := TO_TIMESTAMP(vdat_End - vdat_Begin);   -- this works
    END;
    /
    
    PLS-00382: expression is of wrong type
    

    Thanks for help.

    No, as stated, there will be an interval.

  • calculate the difference between two timestamps

    Hello
    I'm new in PL/SQL and have the following problem. In our application, there are events that insert records into a table with a parameter "processing_date" of type DATE (when they will be processed). For a new type of events, I have to plan the 'processing_date' at 18:00 on the day of insertion, if the event has happened until 18:00 and sysdate if event occurs after 18:00.
    In a pseudo-code that will be
    IF to_char(SYSDATE,'HH24:MI:SS') > 17:59 min 59 s ' THEN - the event took place after 18:00
    processind_date: = sysdate;
    Else - the event has happened until 18:00
    processing_date: = current_timestamp + ('timestamp at 18:00 ' - current_timestamp); -> processing_date: = today at 18:00
    END IF;

    I'll appreciate any help.

    Thank you.

    dariusgeo wrote:
    processing_date: = current_timestamp + ('timestamp at 18:00 ' - current_timestamp); -> processing_date: = today at 18:00

    For 18:00 of the current date, you can do:

     processing_date := TRUNC(SYSDATE)+18/24;
    

    TRUNC will be truncates the current date ' 00:00:00 '.
    Add 18/24 means add 18 hours (1 day / 24 * 18).

    Kind regards.
    Al

    Published by: Alberto Faenza 22 may 2013 22:07

  • difference between two timestamps

    Hi gurus,

    I have a procedure which has 2 input parameters of type timestamp. I need to find the difference between these two stamps and be converted to a timestamp and return. Help, please

    Diifernce between two timestamps is interval. You can not convert the difference between two timestamp timestamp and it makes no sense. What you expect:

    25/06/2014 11:25:37.524000 - 13:07:22.987654 20/06/2014

    SY.

  • Regular expression help please. (extraction of a subset of the string between two markers)

    I haven't used regular expressions before, and I can't find a regular expression to extract a subset of the string between two markers.

    The chain;

    Stuff of header I want
    Stuff of header I want
    Stuff of header I want

    Stuff of header I want
    Stuff of header I want
    Stuff of header I want

    6 ERRORS
                         
    Info I want to line 1
    Info I want line 2
    Info I want line 3
    Info I want to line 4
    Info I want to line 5
    Info I want line 6
    END_ERRORS

    From the string above (it is read from a text file), I try to extract the subset of string between ERRORS 6 and END_ERRORS. The number of errors (6 in this case) can be any number from 1 to 32, and the number of lines I want to extract will correspond with this number. I can provide this number of a caller VI if necessary.

    My current solution, which works, but is not very elegant;

    (1) using Match Regular Expression for the return of the string after you have synchronized the 6 ERRORS

    (2) uses the Regular Expression matches to return all characters before game END_ERRORS of the string returned by (1)

    Is there a way this can be accomplished using 1 Regular Expression Match? If so someone could suggest how, as well as an explanation of the work of the given regular expression.

    Thank you very much

    Alan

    I used a character class to catch any word or whitespace characters.  This put inside parentheses a substring matching the criteria that you can get by developing the node for regular expression matching.  The \d matches the numbers and the two * s repetition of the previous term.  So, \d* will find the '6', as well as "123456".

  • Number of occurrences of a day between two dates

    Hello

    I have a requirement in my code to count the number of times per day occurs between two dates. So, for example if

    start date = January 1, 2014

    end date = March 31, 2014

    The number of times that happens day 1 is three times. That's why I want the result to be 3. My three input parameters are the number of the day, start and end dates. I know that this can be done by parsing through the days and incrementing a counter of sorts, but can it be done without any loop etc?

    Looking for options of

    Thank you

    Birdy

    SELECT COUNT (TO_CHAR (DAYS, 'DD')) IN)

    SELECT DAYS START_DATE + LEVEL 1

    FROM T1

    CONNECT BY LEVEL<=>

    WHERE TO_CHAR (DAYS, 'DD') ='&01';

    Post edited by: Parth272025

  • 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

  • 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;
    
  • Take the time between two values

    Hi people,

    I have a problem and I know idea how to solve... I need help.

    The problem is I want to take the time between two values max as you can see in the chart.

    For example, in the image that I have add

    4.5 - 1 840 = 2.66

    And enter this value in the 'time between mostra '.

    It's that I want...

    But what I think is very complicated, because I don't know how to take the time correctly and does remove...

    Thank you very much

    Any solution?

    Hi jocuma,

    I tried something and hope that helps u.

    Just create two arrays of temperature and voltage. First of all, I'll get the value of the voltage when it is more of a certain value and that same index to get the value of time and store in the shift register.

    When I get the second higher than the limit value, I'll get time and subtract the previous value.

  • Measure the time between two digital pulse

    Hello

    For a non-critical calendar application, I need to measure the time interval between consecutive TTL pulses, ranging from the order of 0.5 s for a few seconds, with a low accuracy of +/-10-50ms. The interval being measured varies between the rising edge of the first pulse and the front of the next and so on.

    I have several input lines I need to deal with. Because it's a critical machination low cost, I don't want to use digital counters for each line, so I work with an acquisition of data USB6008 and have connected the input rows TTL on the digital inputs of the device. Avoiding will be sufficient.

    I found a good example of VI on discussion forums that does almost the same thing, only it uses instead of the DAQ Assistant user input. The VI works including the time the program going on in a while loop. I replaced with the DAQ Assistant output (a channel) user input in the hope that it is still work.

    When I run the program in "run once" mode, it seems to work perfectly. However, in "continuous run" it measures only a very small interval, probably just the time between two samples.  I think it has something to do with the help of a while loop in combination with the DAQ Assistant. Anyone who has any suggestions how to solve this problem?

    Thank you!

    OK... first of all, you should never use the button "run continuously.  I wish that NEITHER would be to eliminate it, but told me that it is sometimes useful for debugging.  If you want your program to run over and over again, use a while loop with a stop"" button.

    If I'm reading your code correctly, you make your initial moment, and then collect data from data acquisition.  When one of the channels is "T", you stop your loop and the end time of capture.  (By the way, why you convert your table to a cluster?  Why not just index the appropriate channel in the table directly?)

    Since you want to capture the time between two consecutive pulses, you need to know when a transition has occurred... i. e when your digital line went from F (no pulse) to T (pulse start).  This will give you your forehead.  Right now, all you're doing is looking for a value T - so you have no way of knowing if you are looking for to the previous impulse again, or a new impetus.  You also burn 100% of your processor with the way you have your programme in place.

    You need a small loop delay so that your VI is not 100% of your hogs CPU time.  Given that you can live with an accuracy of 50msec, what I suggest that you use.

    See attached picture for you give an idea of how to implement.  He will probably need some refining operations, but it should point you in the right direction.

    I hope this helps.

  • How to avoid synchronization of the settings between two computers

    I have two computers (a desktop computer all-in-one DELL and HP laptop).  Log-in at once is my Microsoft account.

    PROBLEM: When I 'customize' screen on one, the same changes are made on the other.   It is not desirable.  How to avoid that, far from making a second Microsoft account?

    To clarify - the ICONS are not duplicated, just any changes made to the themes of office such as colors or backgrounds.

    Thanks in advance!

    Hello Beckinista,

    Thanks for posting your question on the Microsoft community.

    Thank you for your query details.

    I also like the fact you want to avoid synchronization of the settings between two computers.

    I suggest you stop the synchronization of all of the settings between two computers and check if it helps.

    Please see this link:
    Sync between multiple PCs with OneDrive settings
    http://Windows.Microsoft.com/en-us/Windows-8/sync-settings-PCs

    Note: Please see section "to stop all sync settings" in the article above.

    I hope this information helps.

    Please let us know if you need more help.

    Thank you

  • 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

  • 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

  • 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

Maybe you are looking for

  • My iPhone 6 is stuck on the apple logo

    My iPhone 6 is stuck on the apple logo. If I put it to download it just stays like that or it stops and says it has no battery, even when plugged. I can't enter in recovery mode because my home button just stopped working out of the blue. I don't kno

  • Firefox for Android User Agent TROUBLE

    I use Firefox on my S4 Galaxy that has a screen size of 1080 x 1920 and Firefox is that it is a tablet. I'm running a custom 320 DPI. The question is that T-Mobile I am when I'm not. Please, I beg you, do an option to select the User Agent, the USER

  • Want J020dx: Power cord for HP Envy 17 "J020DX question, urgent pls!

    Hi all I have a HP envy 17 "J020dx, and now I'm in Australia, where I couldn't use the previous/power cord, and the problem is I could not find the adapter Plug and will be stuck in this situation for a while. I managed to find another power cord tha

  • SmartWatch 2 compatible with CRA S

    I would like to know if my s Xperia Arc will be compatible with the new Smartwatch 2? My Arc S has already been updated for android 4.1.2. It's only the bluetooth 3.0, which is troubling me. I think that my phone bluetooth 2.1. It will be compatible

  • Landscape of blackBerry Smartphones

    Hi, I'm on os 109 for about 4 months now with no problems until today. The phone won't work on portrait not just of landscape mode. When I do a battery pull it woks for a short period of time only, any suggestions. Thank you