我如何...从日期列表中恢复壁橱日期 [英] How do I...Retrive Closet date from date list
问题描述
我有约会日期
Id日期
1 2-10-2015
1 10- 10-2016
1 3-10-2017
2 2-10-2015
2 10-10-2016
2 3-10-2017
所以我需要的日期最接近当前日期
而不是之前的最近日期即将到来最近的日期
I have date like
Id Date
1 2-10-2015
1 10-10-2016
1 3-10-2017
2 2-10-2015
2 10-10-2016
2 3-10-2017
So i need date that is closest to current date
and not the previous closest date coming closest date
推荐答案
尝试
select TOP 1 id, [Date]
from Avinash
order by Abs(Datediff(dd, getdate(), [Date])), [Date]
由于使用 Abs
$ b,无论是在当前日期之前还是之后,都会找到最接近的日期。 $ b如果有两个日期同样clo se(即在当前日期之前x天和之后x天)然后查询当前返回较早的日期。如果您希望它返回未来日期,则按
This will find the closest date regardless of whether it is before or after the current date due to the use of Abs
If there are two dates equally close (i.e. x days before current date and x days after) then the query currently returns the earlier date. If you want it to return the future date then order by
Abs(Datediff(dd, getdate() Asc, [Date])), [Date] Desc
尝试
Try
SELECT TOP 1 * FROM MyTable
WHERE [Date] > GETDATE()
ORDER BY DATEDIFF(DAY, [Date], GETDATE()) DESC
这篇关于我如何...从日期列表中恢复壁橱日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!