我建立了一个应用程序设计为自助餐厅的在线菜单。但由于某些原因,即使它被填充,它也不会为今天返回任何物品。这是我用来拉出项目的SQL查询:
Select num, FoodItem, Week, Day, Station, Site
from FoodMenu WHERE [Week] = (DATEPART(WEEK, GETDATE()) % 3) + 1
AND [Day] = DATEPART(DW, GETDATE()) -1
星期一设置为Day 1
,星期日设置为Day 7
答案 0 :(得分:4)
-1看起来不正确,因为它会产生以下结果(注意当你想要7时星期日为0):
Day | DOW | Transformed Sunday | 1 | 0 Monday | 2 | 1 Tuesday | 3 | 2 Wednesday | 4 | 3 Thursday | 5 | 4 Friday | 6 | 5 Saturday | 7 | 6
你真正想要的是:
Day | DOW | Transformed Sunday | 1 | 7 Monday | 2 | 1 Tuesday | 3 | 2 Wednesday | 4 | 3 Thursday | 5 | 4 Friday | 6 | 5 Saturday | 7 | 6
最简单的方法是使用一个案例:
[Day] = CASE WHEN DATEPART(WEEKDAY, GETDATE()) = 1 THEN 7 ELSE DATEPART(WEEKDAY, GETDATE()) - 1 END