为什么"where SomeDate between getdate() and DATEADD(m, -1, getdate()))" -
无效?
(我试着计算一些上个月的值)
答案 0 :(得分:1)
因为当您使用between
时, lower 值需要先行。所以,你想要:
where SomeDate between DATEADD(month, -1, getdate()) and getdate()
答案 1 :(得分:0)
您应该使用其他方式
where SomeDate between DATEADD(m, -1, getdate()) and getdate()
答案 2 :(得分:0)
你需要像这样使用它:
WHERE SomeDate BETWEEN DATEADD(m, -1, GETDATE() and GETDATE()
较小的值首先出现,在本例中为DATEADD
答案 3 :(得分:0)
这里有一些例子
DECLARE @ServerDate DATETIME = GETDATE()
我添加Distinct以避免冗余数据字段
SELECT DISTINCT * FROM FooTable
WHERE SomeDate BETWEEN DATEADD(DD, 0, DATEDIFF(DD, 0, @ServerDate)) AND DATEADD(DD, 1, @ServerDate)
DATEADD(DD,0,DATEDIFF(DD,0,@ ServerDate))相当于YYYY-mm-dd 00:00:00.000