日期差异 GETDATE 添加

发布于 2025-01-03 06:08:00 字数 267 浏览 6 评论 0原文

在这种类型的代码中,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

椵侞 2025-01-10 06:08:00

这不是一个日期跨度。

你所拥有的条件实际上只有一个条件:大于。大于号的右侧是 6 天前,因此您的条件与晚于 6 天前的日期的任何日期匹配。换句话说,它不仅仅停留在今天;它还停留在今天。它也包括明天、下周和明年。

AND ( Orders.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -7), 0)
      AND Orders.ShipDate < DATEADD(Day, Datediff(Day,0, GetDate()), 0) )

这才是你真正想要的。它匹配 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.

AND ( Orders.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -7), 0)
      AND Orders.ShipDate < DATEADD(Day, Datediff(Day,0, GetDate()), 0) )

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).

向日葵 2025-01-10 06:08:00

“日期范围的结束”不在您的查询中。

将代码更改为:

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()))

紙鸢 2025-01-10 06:08:00

这应该也有效,并且消除了不必要的 0 天添加:

select *,DATEDIFF(Day, orders.ShipDate, GETDATE()) AS DAYS_SINCE_TODAY
from Orders 
where DATEDIFF(Day, orders.ShipDate, GETDATE()) >= 1 AND --This many days since today
      DATEDIFF(Day, orders.ShipDate, GETDATE()) <= 7     --Going back this many days

This should work too and it eliminates the needless addition of 0 days:

select *,DATEDIFF(Day, orders.ShipDate, GETDATE()) AS DAYS_SINCE_TODAY
from Orders 
where DATEDIFF(Day, orders.ShipDate, GETDATE()) >= 1 AND --This many days since today
      DATEDIFF(Day, orders.ShipDate, GETDATE()) <= 7     --Going back this many days
爱人如己 2025-01-10 06:08:00

我喜欢 BETWEEN 函数。这可能与您使用的 SQL 风格不同,但这就是我使用的。

BETWEEN dateadd(second, 0, dateadd(dd,datediff(dd,7,getdate()),0)) AND dateadd(second,-1, dateadd(dd,datediff(dd,0,getdate()),0))  

I like the BETWEEN function. This may be a different flavor of SQL than you are using, but this is what I use.

BETWEEN dateadd(second, 0, dateadd(dd,datediff(dd,7,getdate()),0)) AND dateadd(second,-1, dateadd(dd,datediff(dd,0,getdate()),0))  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文