title: SQL Server : DATEDIFF exclude certain time within the two dates
body: <p>I'm trying to get the difference between two dates in minutes, is it possible for me to exclude lunch times from this calculation aka 13:30-14:00 purely within my SQL statement. What I'm currently doing is below:</p>
  
  <p>Example row</p>
  
  <pre><code>SetupStart                  StartTime                  SetupTime 
  ----------------------------------------------------------------
  2017-01-23 12:56:42.000     2017-01-23 14:41:06.000     105
  </code></pre>
  
  <p>My current statement:</p>
  
  <pre><code>DATEDIFF(MINUTE, UserData.Orders.SetupStart, UserData.Orders.StartTime)  AS[SetupTime]
  </code></pre>
  
  <p>EDIT:--------------</p>
  
  <p>Here is my current Select statement</p>
  
  <pre><code>SELECT DISTINCT 
                    TOP (100) PERCENT UserData.Resources.Name AS Resource, UserData.Orders.OrderNo, UserData.Orders.StringAttribute4 AS Customer, UserData.Orders.Product, 
                    CEILING(UserData.Orders.OpNo10Quantity) AS OpNo10Quantity, UserData.Orders.NumericalAttribute12 AS Speed, UserData.Orders.SetupStart, UserData.Orders.StartTime, 
                    UserData.Orders.EndTime, CASE ToggleAttribute1 WHEN 1 THEN 'Yes' ELSE 'No' END AS Packed, UserData.Orders.StringAttribute5 AS OrderInstructions, UserData.Orders.NextResource, 
                    UserData.Orders.DatasetId, UserData.Orders_Dataset.name AS ScheduleName, UserData.Orders.ShowOnReport, dbo.tblPreactorExportFullv10.Length AS L, 
                    dbo.tblPreactorExportFullv10.Thickness AS T, dbo.tblPreactorExportFullv10.fWidth AS W,
   DATEDIFF(MINUTE, UserData.Orders.SetupStart, UserData.Orders.StartTime) AS [SAM SetupTime]
  FROM         UserData.Orders INNER JOIN
                    UserData.Resources ON UserData.Orders.Resource = UserData.Resources.ResourcesId AND UserData.Orders.Resource = UserData.Resources.ResourcesId INNER JOIN
                    UserData.Orders_Dataset ON UserData.Orders.DatasetId = UserData.Orders_Dataset.DatasetId AND UserData.Orders.DatasetId = UserData.Orders_Dataset.DatasetId INNER JOIN
                    dbo.tblPreactorExportFullv10 ON UserData.Orders.PartNo = dbo.tblPreactorExportFullv10.ProductCode
  WHERE     (UserData.Orders.DatasetId = 15) AND (UserData.Resources.Name = 'Moulder 6') AND (UserData.Orders.ShowOnReport = 1)
            AND (UserData.Orders.OperationProgress &lt;&gt; 5)
   ORDER BY UserData.Orders.SetupStart
  </code></pre>