SQL WHEN函数-如何修复语法错误?

时间:2019-02-06 13:52:18

标签: sql sql-server case

我是SQL编码的新手,到目前为止,我仅遇到一个错误代码。 在第二行WHEN CONVERT

({rd.254是我的餐桌日期栏)

CASE WHEN CONVERT(date,rd.F254) Between 
CONVERT(date,getdate()-7) AND CONVERT(date, getdate()-1) THEN rd.F64 END AS QtyLastWeek

WHEN CONVERT(date,rd.F254) <= CONVERT(date,getdate()-8) THEN rd.F64 END AS Qty2WeeksAgo

*Screenshot Here*

我正在尝试查找并修复语法。

3 个答案:

答案 0 :(得分:2)

语法错误。如果要选择2列QtyLastWeekQty2WeeksAgo,则必须在QtyLastWeek之后使用逗号,并使用另一个CASE表达式。

SELECT 
    CASE WHEN CONVERT(date,rd.F254) BETWEEN CONVERT(date,getdate()-7) AND CONVERT(date, getdate()-1) THEN rd.F64 END AS QtyLastWeek,
    CASE WHEN CONVERT(date,rd.F254) <= CONVERT(date,getdate()-8) THEN rd.F64 END AS Qty2WeeksAgo

答案 1 :(得分:2)

您缺少逗号和大小写

CASE WHEN CONVERT(date,rd.F254) Between CONVERT(date,getdate()-7) AND CONVERT(date, getdate()-1) THEN 
   rd.F64 
END AS QtyLastWeek,
CASE WHEN CONVERT(date,rd.F254) <= CONVERT(date,getdate()-8) THEN 
   rd.F64 
END AS Qty2WeeksAgo

答案 2 :(得分:0)

您在第二个case语句中缺少case关键字。尝试这个 !

CASE WHEN CONVERT(date,rd.F254) Between CONVERT(date,getdate()-7) AND CONVERT(date,getdate()-1) 
THEN rd.F64 
END AS QtyLastWeek,
CASE   WHEN CONVERT(date,rd.F254) <= CONVERT(date,getdate()-8) 
      THEN rd.F64 
END AS Qty2WeeksAgo