日期差异 GETDATE 添加
在这种类型的代码中,
AND Orders.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -6), 0)
它应该提取日期为 6 天前直到今天的记录。如何让它提取7天前到昨天的记录?
我知道将 -6
更改为 -7
会提取 7 天前的记录,但是哪个变量是日期范围的末尾,所以我可以将其更改为 - 1?
In this type of code,
AND Orders.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -6), 0)
It supposed to pull records with the date 6 days ago, until today. How can I make it pull records from 7 days ago until yesterday?
I know changing -6
to -7
will pull records from 7 days ago, but which variable is the end of the date span so I can change it to -1
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是一个日期跨度。
你所拥有的条件实际上只有一个条件:大于。大于号的右侧是 6 天前,因此您的条件与晚于 6 天前的日期的任何日期匹配。换句话说,它不仅仅停留在今天;它还停留在今天。它也包括明天、下周和明年。
这才是你真正想要的。它匹配 7 天前当天午夜之后的日期以及今天午夜之前的日期(昨天的任何时间)。
It's not a date span.
The condition you have there is really only one condition: greater than. The right side of the greater than is 6 days ago, so your condition matches any date that is later than the date six days ago. In other words, it doesn't stop at Today; it includes tomorrow, next week, and next year, too.
That's what you really want. It matches dates which are later than midnight of the day 7 days ago, and dates which are before midnight today (which is any time yesterday).
“日期范围的结束”不在您的查询中。
将代码更改为:
AND (Orders.ShipDate BETWEEN DATEADD(Day, -1, GetDate()) AND DATEADD(Day, -7, GetDate()))
The "end of the date span" isn't in your query.
Change your code to:
AND (Orders.ShipDate BETWEEN DATEADD(Day, -1, GetDate()) AND DATEADD(Day, -7, GetDate()))
这应该也有效,并且消除了不必要的 0 天添加:
This should work too and it eliminates the needless addition of 0 days:
我喜欢 BETWEEN 函数。这可能与您使用的 SQL 风格不同,但这就是我使用的。
I like the BETWEEN function. This may be a different flavor of SQL than you are using, but this is what I use.