我使用SELECT查询从日历表中获取数据。我想今天的数据,除了星期五,我想要周五,周六和周日的数据。这是我的疑问:
SELECT *
FROM Calendar
WHERE entry_date =
CASE
WHEN DATEPART(dw,GETDATE()) = 6
THEN CAST(GETDATE() AS DATE) OR DATEADD(day, 1, CAST(GETDATE() AS DATE)) OR DATEADD(day, 2, CAST(GETDATE() AS DATE))
ELSE
CAST( GETDATE()AS DATE)
END;
这给了我不正确的语法
答案 0 :(得分:1)
试试这个:
WHERE entry_date = CAST(GETDATE() AS DATE) OR
(DATEPART(dw, GETDATE()) = 6 AND
entry_date IN (CAST(GETDATE() + 1 AS DATE), CAST(GETDATE() + 2 AS DATE))
)
请注意,这会使用+ 2
的简写表示法向datetime
值添加两天。如果您愿意,可以使用dateadd()
代替。
答案 1 :(得分:0)
试试这个:
SELECT *
FROM Calendar
WHERE (DATEPART(dw,GETDATE()) = 6
AND entry_date IN (CAST(GETDATE() AS DATE), DATEADD(DAY, 1, CAST(GETDATE() AS DATE)), DATEADD(DAY, 2, CAST(GETDATE() AS DATE))))
OR (DATEPART(dw,GETDATE()) <> 6
AND entry_date = CAST( GETDATE()AS DATE));