SQL:创建额外列指定送货截止日期时间

时间:2012-10-26 05:53:04

标签: sql sql-date-functions

我正在查询我的数据库以创建一个输出,该输出将指定每个订单的发货截止日期。为此,我想对过去几周的订单进行查询,该订单包含订单日期/时间的列。但是,我还想自动创建另一个名为“Shipping Deadline”的列,该列计算订单生成后24小时的日期/时间。如下;

    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

有一种简单的方法可以在SQL中创建Shipping Deadline列吗?这就是我现在所要做的。

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'

谢谢!

1 个答案:

答案 0 :(得分:1)

你说24小时但是例子显示48小时?

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'

顺便说一下,将来不要使用单引号作为列别名;使用如上所示的方括号。