Click here to Skip to main content
15,911,646 members
Home / Discussions / Database
   

Database

 
GeneralRe: How to take a number which compare nearby some numbers ? Pin
Shameel23-Nov-09 2:16
professionalShameel23-Nov-09 2:16 
AnswerRe: How to take a number which compare nearby some numbers ? Pin
Shameel23-Nov-09 4:00
professionalShameel23-Nov-09 4:00 
GeneralRe: How to take a number which compare nearby some numbers ? Pin
Luc Pattyn23-Nov-09 4:28
sitebuilderLuc Pattyn23-Nov-09 4:28 
GeneralRe: How to take a number which compare nearby some numbers ? Pin
Roger Wright24-Nov-09 19:04
professionalRoger Wright24-Nov-09 19:04 
GeneralRe: How to take a number which compare nearby some numbers ? Pin
Luc Pattyn25-Nov-09 2:21
sitebuilderLuc Pattyn25-Nov-09 2:21 
GeneralRe: How to take a number which compare nearby some numbers ? Pin
Roger Wright25-Nov-09 2:47
professionalRoger Wright25-Nov-09 2:47 
GeneralRe: How to take a number which compare nearby some numbers ? Pin
Luc Pattyn25-Nov-09 2:52
sitebuilderLuc Pattyn25-Nov-09 2:52 
AnswerRe: How to take a number which compare nearby some numbers ? [modified] Pin
Niladri_Biswas23-Nov-09 3:27
Niladri_Biswas23-Nov-09 3:27 
This will work gracefully

declare @t table(num int)
insert into @t
select 12 union all select 43
union all select 31 union all select 35
declare @inputNum int
set @inputNum = 30

select num as [Closest Number] from(
select DENSE_RANK() over(order by abs(num - @inputNum)) [rank]		
		,num from @t
)X
where [rank] = 1


Output:
Closest Number
31


Note: Try with
Input Output
100 43
-1 12
39 43 & 35
(which is correct only because the difference is 4 for both the case). Henceforth I have used Dense_Rank()

Hope this helps

Smile | :)

Niladri Biswas

modified on Monday, November 23, 2009 9:37 AM

GeneralRe: How to take a number which compare nearby some numbers ? Pin
Golden Jing23-Nov-09 21:36
Golden Jing23-Nov-09 21:36 
AnswerRe: How to take a number which compare nearby some numbers ? Pin
Shameel23-Nov-09 4:59
professionalShameel23-Nov-09 4:59 
GeneralRe: How to take a number which compare nearby some numbers ? Pin
Niladri_Biswas23-Nov-09 16:04
Niladri_Biswas23-Nov-09 16:04 
GeneralRe: How to take a number which compare nearby some numbers ? Pin
Shameel23-Nov-09 22:01
professionalShameel23-Nov-09 22:01 
AnswerThanks all of you... Pin
Golden Jing23-Nov-09 21:41
Golden Jing23-Nov-09 21:41 
QuestionSimilarity search in SQL Pin
Jassim Rahma21-Nov-09 9:47
Jassim Rahma21-Nov-09 9:47 
AnswerRe: Similarity search in SQL Pin
T210221-Nov-09 14:17
T210221-Nov-09 14:17 
AnswerRe: Similarity search in SQL Pin
i.j.russell21-Nov-09 14:19
i.j.russell21-Nov-09 14:19 
AnswerRe: Similarity search in SQL Pin
Luc Pattyn21-Nov-09 14:58
sitebuilderLuc Pattyn21-Nov-09 14:58 
AnswerRe: Similarity search in SQL Pin
Niladri_Biswas21-Nov-09 15:44
Niladri_Biswas21-Nov-09 15:44 
AnswerRe: Similarity search in SQL Pin
Shameel22-Nov-09 23:54
professionalShameel22-Nov-09 23:54 
AnswerRe: Similarity search in SQL Pin
David Skelly23-Nov-09 23:05
David Skelly23-Nov-09 23:05 
Questionwhat is the Microsoft SQL Server 2005 Client version? Pin
AJ Hoge20-Nov-09 19:32
AJ Hoge20-Nov-09 19:32 
AnswerRe: what is the Microsoft SQL Server 2005 Client version? Pin
Richard MacCutchan22-Nov-09 11:47
mveRichard MacCutchan22-Nov-09 11:47 
AnswerRe: what is the Microsoft SQL Server 2005 Client version? Pin
T210222-Nov-09 12:13
T210222-Nov-09 12:13 
AnswerRe: what is the Microsoft SQL Server 2005 Client version? Pin
Shameel23-Nov-09 0:01
professionalShameel23-Nov-09 0:01 
AnswerRe: what is the Microsoft SQL Server 2005 Client version? Pin
AJ Hoge23-Nov-09 18:18
AJ Hoge23-Nov-09 18:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.