Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL and some MATH problem

Author  Topic 

matt_calhoon
Posting Yak Master

235 Posts

Posted - 2002-02-22 : 22:28:30
Hi there,

Is there a sql function/command that will return a number that is closest to another number, whether that number is greater or less than the number concerned? For eg;

I want to return the number nearest to 150.

the result list:
120
180
155

so 155 will be returned as it is closest to the 150 number?

Thanks - matt

jbkayne
Posting Yak Master

100 Posts

Posted - 2002-02-23 : 01:31:31
I would just use the following:

select top 1 Number from List
order by abs(Number - @number)


Go to Top of Page

matt_calhoon
Posting Yak Master

235 Posts

Posted - 2002-02-23 : 18:52:00
exactly what I was looking for.

thanks jbkayne.

Go to Top of Page
   

- Advertisement -