Log in

View Full Version : nearest number



cancer10
03-08-2008, 01:41 PM
If you have a column, of data type = INT, with the following numbers
as separate rows.
1
3
4
5
7
10


How can you write a sql statement where you can calculate the
nearest number to any give input.
Example , if the input is 9 ? (the answer is 10)
If the input is 8 (the anwer is 7)

Twey
03-08-2008, 09:03 PM
SELECT * FROM my_table ORDER BY ABS(my_column - my_input) LIMIT 1;

cancer10
03-09-2008, 03:55 AM
what does ABS do?

Twey
03-09-2008, 07:19 AM
abs n | n < 0 = 0 - n
| otherwise = n