• Here's a modified version of the function I posted in here: http://www.sqlservercentral.com/scripts/Working+Hours/111097/

    I removed breaks and lunch, and adapted the times.

    The DATEADD(DD, DATEDIFF(DD, 0, @StartDate), 0) can be replaced by TRUNC() in PL/SQL, but I don't remember what are the replacements for the first functions. You'll have to figure those out.

    The best part is that it doesn't need tables, unless you want to consider holidays.

    /*

    Programmer: Luis Cazares

    Date: 2014-05-22

    Purpose: This function will return working hours between given 2 dates.

    This function also assumes that the working hours are between 7:00 AM and 6:00 PM.

    This function was inspired by Goran Borojevic

    */

    CREATE function [dbo].[CalcWorkingHours] (@StartDate datetime, @EndDate datetime)

    RETURNS table

    AS RETURN

    SELECT ISNULL((((DATEDIFF(dd,@StartDate,@EndDate)-1) --Start with total number of days including weekends

    - (DATEDIFF(wk,@StartDate,@EndDate)*2) --Subtact 2 days for each full weekend

    - (1-SIGN(DATEDIFF(dd,6,@StartDate)%7)) --If StartDate is a Sunday, Subtract 1

    - (1-SIGN(DATEDIFF(dd,5,@EndDate) %7))) * 660) --This will give us full days minus one that we'll complete with following operations

    + (SELECT CASE WHEN @StartDate <= DATEADD(MI, 420, DATEADD(DD, DATEDIFF(DD, 0, @StartDate), 0))

    THEN 660 --if Start Date is earlier than 7:00 then it counts as full day

    WHEN @StartDate >= DATEADD(MI, 1080, DATEADD(DD, DATEDIFF(DD, 0, @StartDate), 0))

    THEN 0 --if Start Date is later than 18:00 then it won't count

    ELSE DATEDIFF(MI, @StartDate, DATEADD(MI, 1080, DATEADD(DD, DATEDIFF(DD, 0, @StartDate), 0))) --Minutes between start date and 18:00

    END

    + CASE WHEN @EndDate <= DATEADD(MI, 420, DATEADD(DD, DATEDIFF(DD, 0, @EndDate), 0))

    THEN 0 --if End Date is earlier than 7:00 then it won't count

    WHEN @EndDate >= DATEADD(MI, 1080, DATEADD(DD, DATEDIFF(DD, 0, @EndDate), 0))

    THEN 660 --if End Date is later than 18:00 then it counts as full day

    ELSE DATEDIFF(MI, DATEADD(MI, 420, DATEADD(DD, DATEDIFF(DD, 0, @EndDate), 0)), @EndDate) --Minutes between 7:00 and end date

    END

    WHERE @StartDate <= @EndDate)

    /* Uncomment to use holidays table

    - ((SELECT count(*)

    FROM holidaystable

    WHERE [Date] BETWEEN @StartDate AND @EndDate) * 7.75)

    */

    ,0) / 60.0 AS WorkingHours

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2