Time difference in hours

Hello

I have 2 dates in my picture of the '2008-12-22 12:20 ' format. Can you please help me to find the time difference in hours, excluding Saturdays and Sundays using SQL.

Thanks for all your suggestions.

Sharan.

Hello Sharan,
If I understand correctly you want to calculate the difference in hours between 2 dates, excluding Saturday and Sunday hours (a kind of working hours, if we worked 24 hours a day from Monday to Friday :)).

If this is the case and you have two dates in two columns on the same record to take a look at the following query
(Loulou gave a solution if you have dates on individual files):

select id
      ,d1
      ,d2
      ,case
       when trunc(d1,'iw') != trunc(d2,'iw') -- dates in different week
       then
          ((trunc(d1,'iw')+5)-least(d1,trunc(d1,'iw')+5))*24 -- first_week
         +((trunc(d2,'iw'))-(trunc(d1,'iw')+7))/7*5*24       -- middle_weeks
         +(least(d2,trunc(d2,'iw')+5)-trunc(d2,'iw'))*24     -- last_week
       else -- dates in the same week
         (least(d2,trunc(d2,'iw')+5)-least(d1,trunc(d1,'iw')+5))*24
       end as working_hours
from dates
;

Below you can see an example (I guess that d1 is always less than d2):

SQL> alter session set nls_language = 'AMERICAN';

Session altered.

SQL> alter session set nls_date_format = 'dd-mon-yyyy hh24:mi:ss';

Session altered.

SQL> create table dates (id number, d1 date, d2 date);

Table created.

SQL> insert into dates values (1,to_date('07012009 00:00:00','ddmmyyyy hh24:mi:ss'),to_date('20012009 00:00:00','ddmmyyyy hh24:mi:ss'));

1 row created.

SQL> insert into dates values (2,to_date('08012009 12:00:00','ddmmyyyy hh24:mi:ss'),to_date('13012009 00:00:00','ddmmyyyy hh24:mi:ss'));

1 row created.

SQL> insert into dates values (3,to_date('11012009 00:00:00','ddmmyyyy hh24:mi:ss'),to_date('18012009 12:00:00','ddmmyyyy hh24:mi:ss'));

1 row created.

SQL> insert into dates values (4,to_date('10012009 09:00:00','ddmmyyyy hh24:mi:ss'),to_date('10012009 22:00:00','ddmmyyyy hh24:mi:ss'));

1 row created.

SQL> insert into dates values (5,to_date('10012009 23:00:00','ddmmyyyy hh24:mi:ss'),to_date('17012009 12:00:00','ddmmyyyy hh24:mi:ss'));

1 row created.

SQL> insert into dates values (6,to_date('12012009 23:00:00','ddmmyyyy hh24:mi:ss'),to_date('13012009 12:00:00','ddmmyyyy hh24:mi:ss'));

1 row created.

SQL> insert into dates values (7,to_date('08012009 23:00:00','ddmmyyyy hh24:mi:ss'),to_date('10012009 12:00:00','ddmmyyyy hh24:mi:ss'));

1 row created.

SQL> insert into dates values (8,to_date('08012009 23:30:00','ddmmyyyy hh24:mi:ss'),to_date('10022009 12:00:00','ddmmyyyy hh24:mi:ss'));

1 row created.

SQL> commit;

Commit complete.

SQL> select id
  2        ,d1
  3        ,d2
  4        ,case
  5         when trunc(d1,'iw') != trunc(d2,'iw') -- dates in different week
  6         then
  7            ((trunc(d1,'iw')+5)-least(d1,trunc(d1,'iw')+5))*24 -- first_week
  8           +((trunc(d2,'iw'))-(trunc(d1,'iw')+7))/7*5*24       -- middle_weeks
  9           +(least(d2,trunc(d2,'iw')+5)-trunc(d2,'iw'))*24     -- last_week
 10         else -- dates in the same week
 11           (least(d2,trunc(d2,'iw')+5)-least(d1,trunc(d1,'iw')+5))*24
 12         end as working_hours
 13  from dates
 14  ;

        ID D1                   D2                   WORKING_HOURS
---------- -------------------- -------------------- -------------
         1 07-jan-2009 00:00:00 20-jan-2009 00:00:00           216
         2 08-jan-2009 12:00:00 13-jan-2009 00:00:00            60
         3 11-jan-2009 00:00:00 18-jan-2009 12:00:00           120
         4 10-jan-2009 09:00:00 10-jan-2009 22:00:00             0
         5 10-jan-2009 23:00:00 17-jan-2009 12:00:00           120
         6 12-jan-2009 23:00:00 13-jan-2009 12:00:00            13
         7 08-jan-2009 23:00:00 10-jan-2009 12:00:00            25
         8 08-jan-2009 23:30:00 10-feb-2009 12:00:00         540,5

8 rows selected.

SQL> 

I hope having tested all cases.

See you soon,.
Davide

Published by: davide gislon on gen / 11/2009 10:05

Tags: Database

Similar Questions

  • A design of query for the conversion of time difference in days, hours, Minutes

    Hi all

    A design of query for the conversion of time difference of time in number of days remaining remaining hours minutes and rest in seconds. Made this one till now. Please suggest for all modifications, until now, it seems to work very well, kindly highlight for any anomaly.

    WITH DATA (startDAte, EndDate, Datediff) AS (SELECT to_date ('2015-10-01 10:00:59 ',' yyyy-mm-dd hh24:mi:ss'), to_date ('2015-20-01 03:00:49 ',' yyyy-mm-dd hh24:mi:ss'), to_date('2015-10-01 10:00','yyyy-dd-mm hh24:mi:ss')-to_date('2015-20-01 03:00','yyyy-dd-mm hh24:mi:ss') FROM dual)

    UNION ALL SELECT to_date ('2015-10-01 10:00:39 ',' yyyy-mm-dd hh24:mi:ss'), to_date ('2015-20-01 03:00:40 ',' yyyy-mm-dd hh24:mi:ss'), to_date('2015-10-01 10:00','yyyy-dd-mm hh24:mi:ss')-to_date('2015-20-01 03:00','yyyy-dd-mm hh24:mi:ss') FROM dual

    UNION ALL SELECT to_date ('2015-11-01 10:30:45 ',' yyyy-mm-dd hh24:mi:ss'), to_date ('2015-11-01 11:00:50 ',' yyyy-mm-dd hh24:mi:ss'), to_date('2015-11-01 10:30','yyyy-dd-mm hh24:mi:ss')-to_date ('2015-11-01 11:00 ',' yyyy-mm-dd hh24:mi:ss') FROM dual

    UNION ALL SELECT to_date ('2015-11-01 09:00:50 ',' yyyy-mm-dd hh24:mi:ss'), to_date ('2015-11-01 10:00:59 ',' yyyy-mm-dd hh24:mi:ss'), to_date('2015-11-01 09:00','yyyy-dd-mm hh24:mi:ss')-to_date ('2015-11-01 10:00 ',' yyyy-mm-dd hh24:mi:ss') FROM dual

    UNION ALL SELECT to_date ('2015-11-01 08:30:49 ',' yyyy-mm-dd hh24:mi:ss'), to_date ('2015-11-01 09:30:59 ',' yyyy-mm-dd hh24:mi:ss'), to_date('2015-11-01 08:30','yyyy-dd-mm hh24:mi:ss')-to_date('2015-11-01 09:30','yyyy-dd-mm hh24:mi:ss') FROM dual

    )

    Select

    trunc ((EndDate-StartDate)) days.

    trunc (((enddate-startdate)-to_number (trunc ((enddate-startdate))) * 24) hours)

    trunc (to_number (((enddate-startdate)-to_number (trunc ((enddate-startdate))) * 24-trunc (((enddate-startdate)-to_number (trunc ((enddate-startdate))) * 24)) * 60) Minutes,))

    (to_number (((enddate-startdate)-to_number (trunc ((enddate-startdate))) * 24-trunc (((enddate-startdate)-to_number (trunc ((enddate-startdate))) * 24)) * 60 - trunc (to_number (((enddate-startdate)-to_number (trunc ((enddate-startdate))) * 24-trunc (((enddate-startdate)-to_number (trunc ((enddate-startdate))) * 24)) * 60)) * 60 seconds))))

    data;

    Thanks for the answers in advance.

    AHA!

    TO_TIMESTAMP expects a string as input, so it first makes an implicit conversion from DATE to a string, in the format of NSL_DATE_FORMAT.

    To convert the TIMESTAMP DATE independently NLS_DATE_FORMAT, use

    CAST ( AS TIMESTAMP)

  • difference in hours between two time zones of differnet

    I have a sysdate (EAST) and a function util_pkg.local_tz returns a different datetime value.
    I need to get the difference in hours between sysdate and another date.

    Select sysdate, util_pkg.local_tz (sysdate, ' Asia/Seoul') of double;

    18/05/2011 11:15:00.000000000 FROM 18/05/2011 10:15 PM + 09:00

    Quest:-find the difference in hours between these two date and then add the difference of hours + sysdate

    FUNCTION local_tz (p_datetime IN TIMESTAMP,
                          p_timezone IN VARCHAR2)
          RETURN TIMESTAMP WITH TIME ZONE
       AS
       BEGIN
          RETURN FROM_TZ(p_datetime, TZ_OFFSET(con_database_timezone))
                         AT TIME ZONE TZ_OFFSET(p_timezone);
       END local_tz;

    When you calculate the diff between TIMESTAMP WITH TZ they both are converted to time UTC (in fact they are stored as UTC). So diff between SYSTIMESTAMP in EST and SYSTIMESTAMP in Seoul will always be 0, as bioth are "now". You need to cast TIMESTAMP WITH TZ 'losing' time zone timestamp:

    SQL> select cast(systimestamp at time zone 'Asia/Seoul' as timestamp) - systimestamp from dual;
    
    CAST(SYSTIMESTAMPATTIMEZONE'ASIA/SEOUL'ASTIMESTAMP)-SYSTIMESTAMP
    ---------------------------------------------------------------------------
    +000000000 13:00:00.000000
    
    SQL>  
    

    SY.

  • Convert time difference in seconds in hours, minutes, and seconds

    Hi all

    I used the following logic to calculate the time difference between 2 passes of time for all the individual lines.

    (floor ((TIME_ACTIONED-TIME_ALLOCATED) * 24) |) ':' || MOD (Floor ((TIME_ACTIONED-TIME_ALLOCATED) * 24 * 60), 60) |': ' | MOD (Floor ((TIME_ACTIONED-TIME_ALLOCATED) * 24 * 60 * 60), 60))

    The user asked a column that displays the SUM of all the differences in time between the report.

    Any help much appreciated!


    Thank you
    Sweta

    Simply the sum of time_actioned - time_allocated, then do the same calculation you make on this sum.

    John

  • How to calculate the time difference

    Hi friends,

    I want to calculate and the sum of the time difference.

    I have two fields in a table. Inside and OUTSIDE. for example.

    OUT IN DIFFERENT

    01:46 15:30?

    01:47 15:45?

    02:50 17:30?

    I want less above figure and the result will appear in different column.

    Help, please

    Kind regards

    Hello

    Assuming that the two columns are the VARCHAR2 data type and "represent" the hours and minutes:

    WITH sample_data AS (SELECT ' 01:46 ' in_col, 15:30 ' out_col FROM DUAL)

    UNION ALL

    SELECT ' 01:47 ' 15:45 ' OF THE DOUBLE

    UNION ALL

    SELECT ' 02:50 ', 17:30 ' OF THE DOUBLE

    )

    SELECT in_col,

    out_col,

    TO_CHAR (TO_DATE ((TO_DATE(out_col, 'HH24:MI')-TO_DATE(in_col, 'HH24:MI')) * 86400, «SSSSS»), "HH24") differ))

    OF sample_data;

    SQL > WITH sample_data AS (SELECT ' 01:46 ' in_col, 15:30 ' out_col FROM DUAL)

    2. ANY TRADE UNION

    3. SELECT ' 01:47 ' 15:45 ' OF THE DOUBLE

    4 UNION ALL

    5. SELECT ' 02:50 ', 17:30 ' OF THE DOUBLE

    6                      )

    7. SELECT in_col,

    out_col 8,.

    9 to_char (TO_DATE ((TO_DATE(out_col, 'HH24:MI')-TO_DATE(in_col, 'HH24:MI')) * 86400, «SSSSS»), "HH24") differ))

    10 FROM sample_data;

    IN_COL OUT_COL DIFFER

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

    01:46 15:30 13:44

    01:47 15:45 13:58

    02:50 17:30 14:40

    SQL >

  • Time difference calculate for changing value

    
    create table tbl ( id number, value1 varchar2(5), value2 varchar2(5), date_changed date);
    
    insert into tbl (id, value1, value2, date_changed)
    
    values (1,'N', 'RM', '16-DEC-2013');
    
    insert into tbl (id, value1, value2, date_changed)
    
    values (1,'RM', 'U', '17-DEC-2013');
    
    insert into tbl (id,value1,value2,date_changed)
    
    values (1,'S', 'N', '16-DEC-2013');
    
    
    
    
    
    
    

    I would like a report showing registration where value1 = RM and the value 2 = you and the time difference he had to change value2 = RM (this is the record where value1 = N and value2 = RM) to value1 = RM

    ID value1 value2 time to change
    1RMU1 days, 0 hours, 0 minutes, 0 seconds

    Can this be achieved? If so, how could I do that?

    Thanks Diana

    Try the below

    SELECT T1.ID id,.

    T1. Description MovieFrame,

    T1. Value1 value2,

    T2. Value2 value2,

    T2.date_changed - t1.date_changed | ' days '.

    EXTRACT (HOUR (TO_TIMESTAMP (t2.date_changed) - TO_TIMESTAMP (t1.date_changed))) | ' hour '.

    EXTRACT (MINUTE SINCE (TO_TIMESTAMP (t2.date_changed) - TO_TIMESTAMP (t1.date_changed))) | ' min '.

    EXTRACT (SECOND (TO_TIMESTAMP (t2.date_changed) - TO_TIMESTAMP (t1.date_changed))) | ' dry ' diff

    OF tbl_new t1, tbl_new t2

    WHERE t2.value2 = 'U '.

    AND t1.value2 = t2.value1

    AND t1.id = t2.id;

  • Is there an easy way to calculate the time difference?

    I am trying to find a way to calculate the time difference between

    here at a certain time

    var settime:Number = 02:35

    and

    now would be getDate();

    I tried something rediculus, but it does not work when I entered a date less than 24 hours, is there a class out there who can just this kind of things

    or y at - it something on the internet that could tell me the difference between different time

    Thank you

    My attempt

    var date: Date = new Date();

    Set timer to what would I like to call the function
    var lastcallH:Number = 22;
    var lastcallM:Number = 50;
    var lastcallS:Number = 0;

    Set up of the 24-hour clock
    var tHour:Number = 24;
    var tMinute:Number = 60;
    var tSecond:Number = 60;

    Get the current time
    var cHour:Number = date.getHours ();
    var cMinutes:Number = date.getMinutes ();
    var cSeconds:Number = date.getSeconds ();

    Set the first variable
    var fH:Number;
    var fM:Number;
    var fS: Number;

    A second set of Variable
    var sH: number;
    var sM:Number;
    var sS:Number;

    Final conversion for the Timer
    var finalMil:Number;

    Time variables that will need to be converted

    If (lastcallH < Thur)
    {
    fH = chorus - Thur;
    fM = cMinutes - tMinute;
    fS = cSeconds - tSecond.
    finalMil = (sH * 60) + (sM * 60) + (sM * 60);
    }
    ElseIf (cMinutes > 0 | cSeconds > 0) {}
    ++ Choir;
    ++ cMinutes;
    fH = chorus - Thur;
    fM = cMinutes - tMinute;
    fS = cSeconds - tSecond.
    }
    else {}
    fH = chorus - Thur;
    fM = cMinutes - tMinute;
    fS = cSeconds - tSecond.
    }

    sH = fH + lastcallH;
    sM = fM + lastcallM;
    sS = fS + lastcallM;

    If (< 0 sH: sM < 0 | sS < 0)
    {
    sH = sH * (-1);
    sM = sM * (-1);
    sS = sS * (-1);
    }
    finalMil = (sH * 60) + (sM * 60) + (sM * 60);

    Allows you to implement a timer
    var yahooTime:Timer = new Timer (finalMil, repeat);
    var repeat: Number = 1;

    yahooTime.start ();

    yahooTime.addEventListener (TimerEvent.TIMER, displayCall);

    function displayCall(event:TimerEvent):void
    {
    trace ("well let's success");
    }

    your first trace() statement is almost certainly not what you want.

    dtDate2 is 08/08/2009

    and it's probably not what you want.  Flash months are zero-based.  That is to say, January corresponding to month 0 and December is the month 11.

  • How can I set the time format 24 hours instead of AM/PM lightning/en? Thank you very much!

    I want to configure lightning with the time format 24 hours rather than the AM/PM format, but cannot find it in the options. Thanks for the help!

    You will need to set it for your operating system. Lightning uses the format of the OS.

  • My Apple Watch works fine, but now the time is 12 hours off - he thinks it's night now - how can I fix?

    My new Apple Watch works very well for several weeks, but today the time is 12 hours off - he thinks it's night now - how can I fix?

    HI -.

    Try the following steps:

    On your iPhone, go to: settings > General > Date time & - check that the value is automatically activated.

    Check that Bluetooth and Wi - Fi are activated on your iPhone:

    -Phone: Settings > Bluetooth - on.

    -Phone: Settings > Wi - Fi - on.

    Check that your iPhone and Apple Watch are connected:

    -When you show the time on your watch, drag upward on your watch dial > drag left / right for the look of settings > check it shows connected and shows a green phone icon. Disable airplane Mode if it is enabled.

    It can help to restart your iPhone and your watch. Turn on both devices off the power together first, and then restart your iPhone before restarting your watch:

    -To switch off your watch: press and hold the button side until you see the cursor off the power; slide it to turn off;

    -To switch on: press and hold the side button until you see the Apple logo.

  • start time of 4 + hours

    Someone knows how to fix a start time of 4 + hours with the following newspapers?

    System.log:

    20 jan 17:40:38 restart [9115]: SHUTDOWN_TIME: 1453340438 303534

    20 jan 17:45:27 localhost bootlog [0]: BOOT_TIME 1453340727 0

    20 January 22:07:49 localhost syslogd [50]: notice of Configuration:

    ASL Module 'com.apple.AccountPolicyHelper' claims the selected messages.

    These messages may not appear in the standard system log files or in the database of the ASL.

    accountpolicy.log:

    Jan 20 17:31:14 (86.556.1) AuthenticationAllowed completed: file '', result: success (0).

    20 January 22:13:27 agent political support account from

    There is literally nothing else in newspapers during the same period.

    iMac 27 inch El Capitan 10.11.3 running 16 GB on memory.

    I really do not have to do a complete cleaning and reinstall.  I have already checked the permissions of drive, reinstalled OSX without wiping the drive and so far nothing works. In the past, it was 2-3 hours of starting time, but 4 + hours is too much.

    You USED to have 2-3 HOURS of waiting for Mac to start?

    Of course it looks like a hard drive failing for me.

    What happen when Mac boots and is running?

  • I have a problem in my iPhone 5 s while I call someone when on-call time at 1 hour of the call is disconnected and say call failed can help you me in this problem.

    I have a problem in my iPhone 5 s while I call someone when on-call time at 1 hour of the call is disconnected and say call failed can help you me in this problem.

    What iOS version you are running. I've heard of it, but I heard no talk for a long time... You can update to iOS.

  • Siri will not stop me give a military even if time the "time of 24 hours' has been disabled in my settings.  How do I change AM/PM?

    Siri will not stop me give a military even if time the "time of 24 hours' has been disabled in my settings.  How do I change AM/PM?

    What you have for country & region and what default language.

  • A3A10 time is 1 hour behind when I turn it on

    The tablet of my Acer A3A10 time is 1 hour late when I turn it on. I went to settings > Date & time > automatic date & time is enabled on the zone is set to Eastern Daylight Time, but it changes berhind everytime I turn it on at a time.  I looked on here to see if anyone else has this problem, but it has not yet found. Maybe someone can help.

    karlasheehan,

    That's what I see time zones:

    Time: GMT - 4:00

    Time Atlantic (Canada): GMT - 3:00

  • My emails arrive with time of 17 hours behind the current time in Outlook Express.

    Original title: Incorrect time of receipt email

    My emails come with time of 17 hours behind right now. My clock IS on the correct time. How to fix this?

    Thank you Shelley.
     

    Check your computer time zone first.

    Control Panel, Date and time, time zone

    Is this right?

    Who is your email provider?

  • calculate the time difference.

    Hello

    How to calculate the time difference by using javascript.

    Example 15.05 and18.35 difference is 2 h 30 min

    BTW: it isn't ~ 2h30min is 3h30min

    Maybe something like this works:

    // result is 3.300000000000002 ~ 3h 30min
    (parseFloat((18.35 - 15.05) / 3600) * 3600)
    

Maybe you are looking for

  • Slow keyboard Smiley: 6s IOS 9.3.2

    Hello world! (Sorry if I make mistakes, English is not my native language). A few days ago my keyboard smiley began to be slow! I have the iPhone 6 and I did update the IOS, I think that there is something wrong. When I switch the keyboard with lette

  • Magical Mac wireless keyboard

    While my magic keyboard works fine load in percentage is not displayed in the keyboard preferences. The percentage of charge is displayed for the mouse and the track pad. Everyone knows about this problem?

  • No dye on the printed items

    Hello, I have a photosmart HP printer and cartridges again, I installed, but the printed paper comes out without any color. The numer is series Photosmart B010. What should do? Thanks in advance

  • Xp network & machines Win 7.

    I have 3 computers, two XPand running a race new windows 7.  One of the XP networking two machines (laptop & desktop) works very well & vice versa machine of windows 7 to a (laptop) XP.  The other XP (desktop) machine has become a boring nightmare th

  • Adobe Flash Player not working not

    Adobe Flash Player, being updated does not work on my Vista!  Weather in Feb & March.  I redownloaded it, the program said it was installed but it does not work.  IT's as if it does not exist.  How do I convince it works?  Is this a problem of intera