Remaining Time Calculation

  • Hi All,

    How to calculate remaining time

    like one day my shecudle is :

    DATE APPFROMTIME APP TO TIME

    07/24/200912:18:00 PM1:18:00 PM

    07/24/200914:30:00 PM15:30:00 PM

    07/24/200917:30:00 PM18:30:00 PM

    but i want remaing free APPtime like....................

    DATE APPFROMTIME APP TO TIME

    07/24/200912:00:00 AM12:17:00 PM

    07/24/20091:17:00 PM14:29:00 PM

    07/24/200916:31:00 PM17:29:00 PM

    07/24/200918:31:00 PM11:59:00 PM

    plz help me.........................

  • Can you explaine how you calcualted those values? I'm totaly missing how you go them.

  • I haven't quite reproduced your required results, but then I don't believe that those results are really what you want!

    Here is the TSQL to set up some test data. If test data is presented in the form of CREATE TABLE and INSERT statements it makes it simpler for other people to set up the test data on their systems, and you are more likely to get a quick response.

    CREATE TABLE #Appointment (

    AppDate datetime,

    AppFromTime datetime,

    AppToTime datetime

    )

    GO

    INSERT INTO #Appointment (AppDate, AppFromTime, AppToTime)

    SELECT '20090724', '12:18:00', '13:18:00' UNION ALL

    SELECT '20090724', '14:30:00', '15:30:00' UNION ALL

    SELECT '20090724', '17:30:00', '18:30:00' UNION ALL

    SELECT '20090725', '09:30:00', '10:15:00' UNION ALL

    SELECT '20090725', '13:15:00', '14:00:00' UNION ALL

    SELECT '20090726', '00:00:00', '02:00:00'

    The query will return the time intervals not occupied by appointments, and also split up into separate days between the date/time range defined by @StartTime and @EndTime.

    DECLARE @StartTime datetime

    DECLARE @EndTime datetime

    SELECT @StartTime = '2009-07-24T00:00:00'

    SELECT @EndTime = '2009-07-27T00:00:00'

    ;WITH cteSchedule (rn, StartTime, EndTime) AS (

    SELECT

    ROW_NUMBER() OVER (ORDER BY S.StartTime, S.EndTime),

    S.StartTime,

    S.EndTime

    FROM (

    SELECT

    AppDate + AppFromTime AS StartTime,

    AppDate + AppToTime AS EndTime

    FROM #Appointment

    UNION ALL

    SELECT

    DATEADD(day, T.N, @StartTime),

    DATEADD(day, T.N, @StartTime)

    FROM dbo.Tally T

    WHERE (T.N BETWEEN 0 AND DATEDIFF(day, @StartTime, @EndTime))

    ) S

    )

    SELECT

    S1.EndTime AS FreeTimeStart,

    S2.StartTime AS FreeTimeEnd

    FROM cteSchedule S1

    INNER JOIN cteSchedule S2 ON (S1.rn = S2.rn - 1)

    WHERE (S1.EndTime < S2.StartTime)

    ORDER BY S1.rn

    The query uses a Tally (aka Numbers) table of sequential integers beween 0 and at least the number of days in the date range.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply