选择有效期为当前日期5天的记录。 [英] select records where the expiry date is 5 days from current date.
本文介绍了选择有效期为当前日期5天的记录。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写查询只返回到期日为当前日期5天的记录。
I want to write the query only returns records where the expiry date is 5 days from current date.
推荐答案
试试这个:
Try this:
DECLARE @ToDate DATETIME = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0)
DECLARE @FromDate DATETIME = DATEADD(dd, -5, @ToDate)
SELECT <Field_list>
FROM <TableName>
WHERE DateTimeField BETWEEN @FromDate AND @ToDate
注意:根据需要更改代码
参见:
DATEADD() [ ^ ]
DATEDIFF() [ ^ ]
所以,你有什么问题?你在哪里被困?
你可能想看看DATEADD()和GETDATE()函数来帮助你进行日期数学
so, what is your problem ? where are you getting stuck ?
you might want to look at DATEADD() and GETDATE() functions for helping you with the date math
在where子句中使用dateadd函数
参见 http://msdn.microsoft.com/en-us/library/ms186819.aspx [ ^ ]
即expirydate> = dateadd(d, 5,getdate())
Getdate()返回当前日期。根据您的操作,您需要小心查询的时间部分。
Use the dateadd function in your where clause
see http://msdn.microsoft.com/en-us/library/ms186819.aspx[^]
i.e where expirydate >= dateadd("d",5,getdate())
Getdate() returns the current date. You will need to be careful of the time part of your query depending on what you are doing.
这篇关于选择有效期为当前日期5天的记录。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文