本文介绍了在SQL Server中查找最近的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表 dbo.X
与 DateTime
列Y
可能有数百条记录。
I have a table dbo.X
with DateTime
column Y
which may have hundreds of records.
我的存储过程有参数 @CurrentDate
,我想查找在列Y
之前的 dbo.X
中的日期小于并最接近 @CurrentDate。
My Stored Procedure has parameter @CurrentDate
, I want to find out the date in the column Y
in above table dbo.X
which is less than and closest to @CurrentDate.
如何找到?
推荐答案
where子句将匹配日期小于@CurrentDate的所有行,并且由于它们是从属的顺序排列的,所以TOP 1将是当前日期的最近日期。
The where clause will match all rows with date less than @CurrentDate and, since they are ordered descendantly, the TOP 1 will be the closest date to the current date.
SELECT TOP 1 *
FROM x
WHERE x.date < @CurrentDate
ORDER BY x.date DESC
这篇关于在SQL Server中查找最近的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!