有关DATE的where子句中的情况

时间:2019-03-07 15:34:57

标签: sql tsql where-clause case-when

您好,我正在尝试创建不考虑周末的报告。基本上今天是2天,不包括周末。但是我对此有疑问。

 Where [Location Code]='MO3TRAILS'
 and
      Case when datepart(weekday,[Shipment Date]+2)='1'then [Shipment Date]+4 
 else
      Case when datepart(weekday,[Shipment Date]+2)= '7' then [Shipment Date]+4 
 else
      [Shipment Date]+2 end end

我一直收到错误消息“ An expression of non-boolean type specified in a context where a condition is expected, near 'end'”。

计划添加此项以在Visual Studio中过滤报表以生成订单。根据今天。这是整个当前查询:

SELECT        CASE WHEN h.[Sent To WMS] = '1' THEN 'YES' ELSE 'NO' END AS 
'Ready to Ship'
        , h.[Internal Comment]
        , CASE WHEN h.[Customer Price Group] <> 'INTERNAL' AND l.[No_] 
LIKE 'P-%' THEN l.[Gen_ Prod_ Posting Group] ELSE 
          CASE WHEN l.[No_] LIKE 'H-%' THEN l.[Gen_ Prod_ Posting Group] 
ELSE '' END END AS 'POS/Repack'
        , h.No_
        , h.[Ship-to Name]
        , h.[Bill-to Name]
        , l.[Location Code]
        , h.[Pool Load]
        , h.[Shipping Agent Code]
        , l.No_ AS 'SKU'
        , CAST(ROUND(l.[Quantity (Base)], 0) AS INT) AS 'QTY'
        , CAST(ROUND(m.[Qty_ per Unit of Measure], 0) AS INT) AS 'ITEMS 
PER PALLET'
        , CAST(CONVERT(varchar, h.[Shipment Date], 101) AS date) AS 'Slot 
Date'
        , h.[Ship-to County], ROUND(l.[Gross Weight], 0) AS 'Weight'
        , CASE WHEN l.[No_] LIKE 'F-00%' THEN FLOOR(l.[Quantity (Base)] / 
m.[Qty_ per Unit of Measure]) ELSE 0 END AS [Full Pallet]
        , CASE WHEN l.[No_] LIKE 'F-00%' THEN CAST(ROUND(l.[Quantity 
(Base)], 0) AS INT)- FLOOR(l.[Quantity (Base)] / m.[Qty_ per Unit of 
Measure]) * CAST(ROUND(m.[Qty_ per Unit of Measure], 0) AS INT) ELSE 0 END 
AS Picks
        , l.[Quantity (Base)] / m.[Qty_ per Unit of Measure] AS [Pallet 
Spots]

FROM            DATASERVER.[Dynamics NAV].dbo.[Sales Header] AS 
h INNER JOIN
            DATASERVER.[Dynamics NAV].dbo.[Sales Line] AS l ON 
h.No_ = l.[Document No_] INNER JOIN
            DATASERVER.[Dynamics NAV].dbo.[Item Unit of Measure] 
AS m ON l.No_ = m.[Item No_]

WHERE        (l.[Location Code] = 'MO3TRAILS') 
          AND (m.Code = 'PALLET') 
          AND (l.[Location Code] <> 'ECW') 
          AND (m.[Item No_] <> 'D-000006') 
          AND (m.[Item No_] <> 'D-000008')

ORDER BY 'Slot Date'

3 个答案:

答案 0 :(得分:0)

这是我想您要实现的目标。如果今天加上2天是周末,那么您希望输入带有4天发货日期的条目,如果今天加上2天是工作日,则想要带有2天发货日期的条目。如果我是正确的,那么以下Where语句应该可以解决问题。

    WHERE 
[Shipment Date] =
    case when datepart(weekday,getdate() + 2) in (1,7) then getdate() + 4
    else  getdate() + 2 end

答案 1 :(得分:0)

此代码是WHERE子句的一部分,这意味着此处的所有内容都必须解析为布尔值“是”或“否”。

CASE语句创建。但是,SQL语言没有真正的布尔值。它只有比较运算符。因此,CASE本身不能产生完整的布尔表达式;它只能产生比较运算符一侧使用的值。

对于这个问题,我们的代码结构如下:

WHERE column = value AND CASE

如所示,这不是值。 CASE表达式的结果必须与某物进行比较,例如:

 WHERE column = value AND CASE < column/value

此外,在生成这些值时,CASE表达式可以包含多个WHEN部分。此处无需嵌套多个CASE表达式。更好的是,我们有一个IN()子句,那么每个WHEN的第一部分是相同的,而后半部分是恒定的。

答案 2 :(得分:0)

好。因此,我终于可以使用以下代码进行整理:

          WHERE h.[Shipment Date]= CASE WHEN datepart(weekday,GETDATE()+2)IN (1) THEN (datediff(DAY,-4,getdate()))
          ELSE CASE WHEN datepart(weekday,GETDATE()+2)IN (7) THEN (datediff(DAY,-4,getdate())) ELSE (datediff(DAY,-2,getdate())) END END

使用此方法返回h。[发货日期]为19/3/12的所有行,即= getdate()+ 4。