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 978005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:59:42+00:00 2026-05-16T03:59:42+00:00

I have an MSSQL query that runs order productivity for a time period. Basically

  • 0

I have an MSSQL query that runs order productivity for a time period. Basically it calculates how long an order takes from start date to completion date in minutes.

What I am doing is getting the date and timestamp where the order is created(a) and getting the date and time stamp where the order is completed(b) and subtracting them then multiplying them by 1440 to get the minutes between the 2 dates.

,FLOOR(((CAST(b.[AuditInsertTimestamp2] AS FLOAT)) - (CAST(a.[AuditInsertTimestamp2] AS FLOAT))) * 1440) AS [Minutes]

This works perfectly fine but this also counts the time that our business is closed. What I want to do is just return how much time between these dates is taken while our business is open (6:30AM – 5:00PM). The dates can span multiple days or just a single day. Any ideas?!?!?!

Thanks!!!!

  • 1 1 Answer
  • 0 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-16T03:59:43+00:00Added an answer on May 16, 2026 at 3:59 am

    Here is the final queries if anyone cares to know….

    /****** Object:  UserDefinedFunction [dbo].[udf_TEST]    Script Date: 08/11/2010 16:49:06 ******/
        SET ANSI_NULLS ON
        GO
        SET QUOTED_IDENTIFIER ON
        GO
        CREATE FUNCTION [dbo].[udf_TEST]  
        (  
            @StartDate DATETIME,  
            @EndDate DATETIME  
        )  
        RETURNS VARCHAR(MAX)  
    
        --DECLARE @StartDate DATETIME  
        --DECLARE @EndDate DATETIME  
        --SET @StartDate = '2010-07-06 14:46:37.577'   
        --SET @EndDate = '2010-07-09 09:04:31.290'  
        BEGIN  
        DECLARE @FinalMinutes AS DECIMAL  
        IF (CONVERT(VARCHAR(13), @StartDate, 114) < CONVERT(VARCHAR(13), @EndDate, 114))  
        BEGIN  
        DECLARE @NonWorkTime1 INT  
        SET @NonWorkTime1 = 780  
        --How many minutes are between order start and end time including non working time  
        DECLARE @AllMins1 INT   
        --Declares how many minutes are in a day and makes it float to get remainder minutes when divided  
        DECLARE @MinsInDay1 DECIMAL  
        SET @MinsInDay1 = 1440.0  
        --Finds how many minutes are between start and end time excluding weekends and assignes to variable   
        SET @AllMins1 = ((DATEDIFF(mi, @StartDate, @EndDate))   
          -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)    
          -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60)  
          -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60))   
        --Calculates how many days have elapsed in the minutes that the order has taken  
        DECLARE @MinDays1 INT  
        SET @MinDays1 = (@AllMins1/@MinsInDay1)  
        --Subtracts complete day non worked minutes from final minutes between orders  
        SET @FinalMinutes = (@AllMins1 - (@MinDays1 * @NonWorkTime1) + 360 + 420)  
        END  
        ELSE  
        BEGIN   
        --How many minutes a day are not worked for trips  
        DECLARE @NonWorkTime INT  
        SET @NonWorkTime = 780  
        --How many minutes are between order start and end time including non working time  
        DECLARE @AllMins INT   
        --Declares how many minutes are in a day and makes it float to get remainder minutes when divided  
        DECLARE @MinsInDay DECIMAL  
        SET @MinsInDay = 1440.0  
        --Finds how many minutes are between start and end time excluding weekends and assignes to variable   
        SET @AllMins = ((DATEDIFF(mi, @StartDate, @EndDate))   
          -(((DATEDIFF(wk, @StartDate, @EndDate) * 2) * 24) * 60)    
          -(((CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END) * 24) * 60)  
          -(((CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END) * 24) * 60))   
        --Calculates how many days have elapsed in the minutes that the order has taken  
        DECLARE @MinDays INT  
        SET @MinDays = (@AllMins/@MinsInDay)  
        --Subtracts complete day non worked minutes from final minutes between orders  
        SET @FinalMinutes = (@AllMins - (@MinDays * @NonWorkTime))  
        END 
        RETURN -(@FinalMinutes) 
        END  
    

    In my Main query I have a case statment that if its less than a day between @StartDate and @Enddate it just subtracts the time otherwise if its more than a day or if its less than a day but its less than 24 hours it sends it to this funtction. Everything seems to work Perfect!!!

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

Sidebar

Related Questions

For example I have a mysql query that gets some data. Then runs another
I have a php script that runs a mysql query, then loops the result,
I have a MySQL query that I use to retrieve random rows from a
I have a MySQL query that: gets data from three tables linked by unique
I have a MySQL query that runs via cron every 30 minutes to delete
I have the following MySQL query that I execute from a .php page SELECT
I have a very large and complex MySQL query that gets run from a
Stupid problem, I have a function that runs a query several times in a
I have two queries that are basically the same: OLD TRANSACTIONS QUERY SELECT t.payment_method,
I have a mysql query that runs in a php file that outputs a

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.