我有一个查询需要选择不同的时间段。逻辑是:
以下是我的查询:
Select *
From ...
WHERE
(DATENAME(DW,GETDATE())= 'Tuesday'
AND (<@SalesDate> BETWEEN DATEADD(DAY, -3, GETDATE()) and DATEADD(DAY, -1, GETDATE())))
OR
(DATENAME(DW,GETDATE())<> 'Tuesday'
AND (<@SalesDate> BETWEEN DATEADD(DAY, -1, GETDATE())and DATEADD(DAY, -1, GETDATE())))
它没有返回我想要的结果。有人可以帮帮我吗?谢谢。
答案 0 :(得分:0)
BETWEEN
有点挑剔。请参阅此question。
GETDATE()
将返回今天的时间。你需要剥掉它。
-- Today's date, stripped of time
DECLARE @Today datetime = CONVERT(datetime,CONVERT(date,GETDATE()))
Select *
From ...
WHERE
(DATENAME(DW,GETDATE())= 'Tuesday'
AND (@SalesDate >= DATEADD(DAY, -3, @Today) and @SalesDate < DATEADD(DAY, -1, @Today)))
OR
(DATENAME(DW,GETDATE())<> 'Tuesday'
AND (@SalesDate >= DATEADD(DAY, -1, @Today) and @SalesDate < DATEADD(DAY, -1, @Today)))