【问题标题】:How to turn null date values to varchar and use where statement in sql如何将空日期值转换为 varchar 并在 sql 中使用 where 语句
【发布时间】:2020-11-05 06:36:02
【问题描述】:

我被告知要获得 1996 年 5 月 5 日之后的所有订单,包括未发货的订单。
NULL 值应为“未发货”。它有效,但我无法再比较日期了。
我确信有一种方法可以正确地做到这一点,我不知道。有什么想法吗?

SELECT [OrderID] AS 'Order Number', 
CASE 
    WHEN [ShippedDate] IS NULL THEN 'Not Shipped' 
END AS 'Shipped Date' 
FROM Orders
WHERE [ShippedDate] IS NULL OR [ShippedDate] > '05/06/1996' -- This line doesn't work

【问题讨论】:

  • 试试默认日期格式'1996-05-06'
  • @Juergen - 这可能是问题的答案。然而,正如 Aaron Bertrand 向我指出的那样,这也是一种模棱两可的日期格式(我也做了同样的假设,认为它是可以的)。有关信息,请参阅stackoverflow.com/a/63958638/14267425 上的 cmets,有关最近的示例,请参阅 stackoverflow.com/questions/64679099/…,我建议您改用 '19960506'
  • 另外,需要根据所选日期获取正确的关系运算符(> 或 >=),例如,“1996 年 5 月 5 日之后制造”将是 >= '19960506> '19960505,但不是 @ 987654328@,因为后者将在 5 月 5 日跳过这些。
  • 我忘了说问题是非空值变为空。我试图做 ELSE [ShippedDate] 但它抛出了一个异常
  • 来自case: "ELSE else_result_expression 如果没有比较运算结果为 TRUE,则返回表达式。如果省略此参数且没有比较运算结果为 TRUE,则 CASE 返回空。”

标签: sql tsql northwind


【解决方案1】:

ELSE [ShippedDate] 添加到case 表达式中将不起作用,正如@HABO 指出的那样,由于data type precedence 的规则将导致'Not Shipped' 无法成功转换为匹配ShippedDate 的数据类型。假设 ShippedDate 是日期时间数据类型。

运行以下示例会引发该异常:

DECLARE @testdata TABLE
    (
        [OrderID] INT
      , [ShippedDate] DATETIME
    );

INSERT INTO @testdata (
                          [OrderID]
                        , [ShippedDate]
                      )
VALUES ( 1, GETDATE())
     , ( 2, NULL );

SELECT [OrderID]
     , CASE WHEN [ShippedDate] IS NULL THEN 'Not Shipped'
            ELSE [ShippedDate]
       END AS 'Shipped Date'
FROM   @testdata
WHERE  [ShippedDate] IS NULL
       OR [ShippedDate] > '1996-05-06'; --use a standard data format here

你得到:

正在发生的事情是 SQL Server 根据这些规则将 'Not Shipped' 转换为 'Shipped Date' 列的数据类型,该值不是有效的日期时间并引发错误。

几个选项,将其添加为另一列,您可以在其中返回 ShippedDate 但随后有类似状态列的内容:

SELECT [OrderID]
     , [ShippedDate] --Give the shipped date as it's own separate column
     , CASE WHEN [ShippedDate] IS NULL THEN 'Not Shipped'
            ELSE 'Shipped' --Then have a Shipped status
       END AS 'Shipped Status'
FROM   @testdata
WHERE  [ShippedDate] IS NULL
       OR [ShippedDate] > '1996-05-06'; --use a standard data format here

为您提供以下结果:

OrderID     ShippedDate             Shipped Status
----------- ----------------------- --------------
1           2020-11-05 09:35:32.653 Shipped
2           NULL                    Not Shipped

如果它们必须在同一列中,那么您可以在语句的 ELSE 部分转换 ShippedDate,使用 FROMAT() 它将为您转换,然后您可以定义 DATETIME 值的格式返回:

SELECT [OrderID]
     , CASE WHEN [ShippedDate] IS NULL THEN 'Not Shipped'
            ELSE FORMAT([ShippedDate], 'yyyy-MM-dd')
       END AS 'Shipped Date'
FROM   @testdata
WHERE  [ShippedDate] IS NULL
       OR [ShippedDate] > '1996-05-06'; --use a standard data format here

给你结果:

OrderID     Shipped Date
----------- --------------
1           2020-11-05
2           Not Shipped

【讨论】:

    【解决方案2】:

    试试这个!

     SELECT [OrderID] AS 'Order Number', 
        CASE 
            WHEN [ShippedDate] IS NULL THEN 'Not Shipped' 
            else [ShippedDate]
        END AS 'Shipped Date' 
        FROM Orders
        WHERE [ShippedDate] IS NULL OR [ShippedDate] > '05/06/1996'
    

    【讨论】:

      【解决方案3】:

      使用标准日期格式:

      WHERE ShippedDate IS NULL OR
            ShippedDate > '1996-05-06'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-10
        • 2020-08-21
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-20
        相关资源
        最近更新 更多