Search code examples
sqlsql-date-functions

SQL : Create Extra Column Specifying Shipping Deadline Date Time


I am querying my DB to make an output that will specify the shipping deadlines for each order. To do this I would like to do a query on a past weeks orders having a column for the date/time of the order. However, I would also like to automatically create another column called "Shipping Deadline" which calculates the date/time 24 hours after the order was made. Like follows;

    Order ID  |  Payment Amount   |   Order Date               |    Shipping Deadline 
    1001         $500                 10/15/2012 10:01:00 AM        10/17/2012 10:01:00 AM
    1002         $200                 10/16/2012 7:37:00 AM         10/18/2012 7:37:00 AM

Is there an easy way do create the Shipping Deadline column in SQL? This is what I have got up to now.

SELECT OrderDate AS 'Order Date', OrderID AS 'Order ID', PaymentAmount AS 'Payment Amount', TotalShippingCost AS 'Total Shipping Cost'
FROM Orders
WHERE OrderDate BETWEEN '10/15/2012 00:00:00' and '10/21/2012 00:00:00'

Thanks!


Solution

  • You say 24 hours but the example shows 48 hours?

    SELECT OrderDate AS [Order Date],
           OrderID AS [Order ID],
           PaymentAmount AS [Payment Amount],
           TotalShippingCost AS [Total Shipping Cost],
           DateAdd(hour, 48, OrderDate) [Shipping Deadline]
      FROM Orders
     WHERE OrderDate BETWEEN '10/15/2012 00:00:00' and '10/21/2012 00:00:00'
    

    By the way, don't use single-quotes for column aliases in future; use the square brackets as shown above.