Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3455074
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:34:09+00:00 2026-05-18T09:34:09+00:00

Let me explain the situation I have two tables: -One is to store the

  • 0

Let me explain the situation

I have two tables:

-One is to store the equipment faults, with their own Start Date and End Date.

-Other to store the breaks, each break have a Start Date and End Date.

For example the fault begin at 9:10 and finish at 9:16. The break begin at 9:14 and finish at 9:18, The fault time is 4 minutes And 2 minutes of the fault don’t count because ocurred on a break. I need get the “2 minutes” value to include it in a report

To calculate it, I use a Scalar-valued Function:

DECLARE @Time int;

SET @Time = 0;

IF (@BreakStartDate > @FaultStartDate) AND (@BreakEndDate < @FaultEndDate)
    SET @Time = DATEDIFF(SECOND, @BreakStartDate, @BreakEndDate);

ELSE IF (@FaultStartDate > @BreakStartDate) AND (@FaultEndDate < @BreakEndDate)
    SET @Time = DATEDIFF(SECOND, @FaultStartDate, @FaultEndDate);

ELSE IF (@FaultStartDate < @BreakStartDate) AND ((@FaultEndDate > @BreakStartDate) AND (@FaultEndDate < @BreakEndDate))
    SET @Time = DATEDIFF(SECOND, @BreakStartDate, @FaultEndDate);

ELSE IF (@FaultEndDate > @BreakEndDate) AND ((@FaultStartDate > @BreakStartDate) AND (@FaultStartDate < @BreakEndDate))
    SET @Time = DATEDIFF(SECOND, @FaultStartDate, @BreakEndDate);

RETURN @Time

I need to validate all scenarios, if the fault start first and finish on the break, etc…

My question is, exist a function that do this automatically?

or a more elegant solution?

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:34:10+00:00Added an answer on May 18, 2026 at 9:34 am

    I was doing something similar with a time entry tool and needed to detect overlap between entered times. If all you really need is “I have a fault. There will be 1 Break during the Fault period. Give me the amount of time during which they’re co-incident”, then your function is on the right track.

    When I did mine, I found exploring test cases visually was better than a truth table, and both were better than prose or code.

    Case 1:                            Case 2:
    Fault:  |-------|                  Fault:  |----------|
    Break:              |-------|      Break:        |----------|
    
    Case 3:                            Case 4:
    Fault:       |----------|          Fault:  |----------|
    Break:  |----------|               Break:    |------|
    

    …etc.

    As for doing this elegantly, I’d point out that if there is any overlap between the two lapses, you always wind up wanting the larger start date. Like so:

    Start Dates:
    Fault:    |----     ...  |----  ...     |----
    Break:       |----  ...  |----  ...  |----
    Verdict:    break        either        fault
    

    …and similar for the smallest end date. Because our zone of interest is when both have started and neither have finished. So your logic can become:

    DECLARE @Time int, @biggestStart datetime, @smallestEnd datetime;
    SET @Time = 0;
    
    IF (@BreakStartDate < @FaultEndDate) AND (@BreakEndDate > @FaultStartDate)
    BEGIN
        -- We have overlap.
        -- Poor man's Math.Max and Math.Min:
        SET @biggestStart = CASE WHEN @FaultStartDate > @BreakStartDate THEN @FaultStartDate ELSE @BreakEndDate END;
        SET @smallestEnd = CASE WHEN @FaultEndDate < @BreakEndDate THEN @FaultEndDate ELSE @BreakEndDate END;
    
        SET @Time = DATEDIFF(SECOND, @biggestStart, @smallestEnd);
    END
    RETURN @Time
    

    If I had to do what you stated, this is probably what I’d go with.

    That said, in analyzing your question, I’m wondering if you don’t have to worry about multiple breaks per fault, if you are trying to do this for all faults in a result set, if you’re aggregating “time per equipment per week” or something. If so, you want another approach. But that’s another answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

let me explain my current situation i have a SharePoint site lets say it
Let me explain the situation before I ask the question. I have a site,
Let me explain my question by posing a hypothetical situation. Lets start with a
Let me explain my situation. I have made a user control that contains an
Let me explain my situation, I have a list of checkboxes in a fieldset,
Let me explain my situation. Currently, I have a lot of applications running on
Imagine I have a situation where I need to index sentences. Let me explain
Alright let me explain my situation first: I am part of an organization that
I know the question sound's a bit weird. Let me explain the situation: I
Let me explain what I mean : Let's say we have a menu with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.