Datediff GETDATE添加 [英] Datediff GETDATE Add

查看:94
本文介绍了Datediff GETDATE添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种类型的代码中,

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

它应该提取6天前的记录,直到今天。我如何才能使它从7天前直到昨天拉记录?

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?

我知道将 -6 更改为 -7 将从7天前提取记录,但是哪个变量是日期范围的结束,因此我可以将其更改为 -1

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?

推荐答案

这不是日期范围。

条件您实际上只有一种条件:大于。大于6天前的右侧,因此您的条件匹配的日期晚于6天前的任何日期。换句话说,它不止于今天;

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

这就是您真正想要的。它匹配7天前一天的午夜之后的日期和今天(昨天的任何时间)午夜之前的日期。

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

这篇关于Datediff GETDATE添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆