繁体   English   中英

在where子句的getdate()和DATEADD(m,-1,getdate()))之间

[英]between getdate() and DATEADD(m, -1, getdate())) on where clause

为什么"where SomeDate between getdate() and DATEADD(m, -1, getdate()))" -不起作用? (我尝试计算上个月的一些值)

因为当您使用between较低的值需要先走。 所以你要:

where SomeDate between DATEADD(month, -1, getdate()) and getdate()

您需要像这样使用它:

WHERE SomeDate BETWEEN DATEADD(m, -1, GETDATE() and GETDATE() 

较小的值排在最前面,在这种情况下为DATEADD

你应该用其他方式

where  SomeDate between   DATEADD(m, -1, getdate()) and getdate() 

这里有一些例子

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM