简体   繁体   English

sql今天比较日期时间

[英]sql compare datetime today

I hope anyone can translate my abstract query. 我希望任何人都可以翻译我的抽象查询。

I want to select * from TABLE where ( [MYDATETIMEROW] < (TODAY - 3 Days)). 我想从TABLE中选择*([MYDATETIMEROW] <(今天 - 3天))。

Does I have to Convert, cast or use datepart or anything else?.. im confused. 我是否必须转换,投射或使用datepart或其他任何东西?..我很困惑。

Are there simple rules? 有简单的规则吗? I would'nt have problems to do that with linq but simple sql I learned just hardly. 我不会有问题用linq这样做,但简单的sql我几乎没学到。

Thank you and best regards. 感谢你并致以真诚的问候。

In simple terms: 简单来说:

Select * from Table where MyDateTimeRow < dateadd(dd,-3,getdate())

But using getdate() will provide both a date and a time, experience says that this is unlikely to be exactly what you want - you might want to strip the time down and just consider the date portion 但是使用getdate()将同时提供日期和时间,经验表明这不太可能完全符合您的要求 - 您可能希望将时间缩短并仅考虑日期部分

Select * From Table where MyDateTimeRow < dateadd(dd, datediff(dd, 0, getdate()) - 3, 0)

您希望DateAdd函数操作日期,并希望GetDate函数获取当前日期:

SELECT * FROM MyTable WHERE [MyDateTimeRow] < DateAdd(dd, -3, GetDate())

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

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