I’m aware of the ABS and Round functions in SQL Server but I believe my problem is a little different and I’m not sure how to use these to achieve the desired result.
Say I have a number: 8000
And I have a query that returns this list of numbers: 0, 5000, 10000, 15000
If I use the ABS function with this list e.g
DECLARE @target as INT
SET @target = 8000
SELECT TOP(1) @result AS Number
FROM dbo.Numbers
ORDER BY ABS(Number - @target)
I get 10000
Which is expected
But how can I make this return 5000 i.e. I always get the result rounded down?
Understanding your question to mean you want the nearest match without going over:
Add a
WHERE Number <= @target.