[英]Get closest previous date T-SQL
I need to get select the closest previous date to a date parameter. 我需要选择最接近日期参数的上一个日期。 Currently I am selecting the closest either side of the date using this code:
目前,我正在使用此代码选择日期中最接近的任一边:
SELECT TOP 1 equities, fund, e_date
FROM tbl_assetmix
WHERE fund = @fund_code
ORDER BY ABS(DateDiff(dd, e_date, @statementdate)) asc
I know it is really simple but can someone suggest how I would be able to select the closest date prior to the @statementdate parameter? 我知道这真的很简单,但是有人可以建议我如何选择@statementdate参数之前的最近日期吗?
Thanks, Tristan 谢谢特里斯坦
Select just lines where date is prior to @statementdate: 仅选择日期在@statementdate之前的行:
SELECT TOP 1 equities, fund, e_date
FROM tbl_assetmix
WHERE fund = @fund_code
AND e_date < @statementdate
ORDER BY ABS(DateDiff(dd, e_date, @statementdate)) asc
or replace the 或更换
AND e_date < @statementdate
with 与
AND e_date <= @statementdate
if the same day is allowed. 如果允许同一天。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.