title: Get orders from 12 hours ago SQL Server
body: <p>I'm trying to pull records from a SQL Server database where the <code>OrderDate</code> (<code>datetime</code>) was 12 hours ago. Ideally I'd like to do the whole hour.. for instance..</p>
  
  <p>If I run the query now, at 21.38 (my time) the query would pull records that have the <code>datetime</code> of  9.00 - 10.00 I'm not bothered about the actual date as such, just orders from 12 hours ago.. this query will run every hour, so the next time it ran it would run at 22.38 and would pull orders from 10.00 - 11.00..</p>
  
  <p>This is what I've tried:</p>
  
  <pre><code>SELECT TOP (100) PERCENT 
      dbo.[Order].OrderID, 
      dbo.Customer.Forename, 
      dbo.Customer.Surname, 
      dbo.Customer.Email, 
      dbo.[Order].OrderDate, 
      dbo.[Order].OrderStatusID, 
      dbo.[Order].WebsiteID, 
      dbo.Addresses.CountryID
  FROM dbo.[Order] 
  INNER JOIN dbo.Customer 
      ON dbo.[Order].CustomerID = dbo.Customer.CustomerID
  INNER JOIN dbo.Addresses 
      ON dbo.[Order].BillingAddressID = dbo.Addresses.AddressID 
      AND dbo.[Order].DeliveryAddressID = dbo.Addresses.AddressID
      AND dbo.Customer.CustomerID = dbo.Addresses.CustomerID
  WHERE (dbo.[Order].WebsiteID IN (1, 2, 8, 12)) 
    AND (dbo.[Order].OrderStatusID = 1) 
    AND (dbo.[Order].OrderDate &gt;= DATEADD(hour, - 12, GETDATE())) 
    AND (dbo.[Order].OrderDate &lt;= DATEADD(hour, - 11, GETDATE()))
  </code></pre>
  
  <p>Update: </p>
  
  <p>This is the correct query I'm currently trying</p>
  
  <pre><code>SELECT     TOP (100) PERCENT dbo.[Order].OrderID, dbo.Customer.Forename,     dbo.Customer.Surname, dbo.Customer.Email, dbo.[Order].OrderDate,     dbo.[Order].OrderStatusID, dbo.[Order].WebsiteID, 
                         dbo.Addresses.CountryID
  FROM         dbo.[Order] INNER JOIN
                        dbo.Customer ON dbo.[Order].CustomerID = dbo.Customer.CustomerID     LEFT OUTER JOIN
                        dbo.Addresses ON dbo.[Order].DeliveryAddressID =     dbo.Addresses.AddressID AND dbo.Customer.CustomerID = dbo.Addresses.CustomerID
  WHERE     (dbo.[Order].WebsiteID IN (1, 2, 8, 12)) AND (dbo.[Order].OrderStatusID = 1)     AND (dbo.[Order].OrderDate &gt;= DATEADD(Hour, DATEDIFF(Hour, 0, GETDATE()) - 12, 0))     AND 
                        (dbo.[Order].OrderDate &lt; DATEADD(Hour, DATEDIFF(Hour, 0,     GETDATE()) - 11, 0))
  </code></pre>