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 2005 Forums
 Transact-SQL (2005)
 SELECT record with closest price

Author  Topic 

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2007-07-25 : 09:12:15
Simple table.

ID Price
1 24156
2 1600
3 9787
4 78776
5 2000

I need to return the id where the price is nearest to 1700, in this example I would want id 2

Can it be done?

Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-07-25 : 09:40:19
[code]select top 1 *
from simple_table
order by abs(1700 - Price)[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2007-07-25 : 10:08:53
Cheers!
Go to Top of Page
   

- Advertisement -