gmhk
gmhk

Reputation: 15960

Query to find given number belongs to any range in the given table

I have a table as

Name(String)    fromRange(int)      toRange(int)
abc             15160               15180
bhy             12510               12515

Now When I input the Number as 12514

my database query should return the value stating that it belongs to the following range 12510 to 12515 or it belongs to the following bhy Name

Scenario 2

If I enter the 12530 it should return 0, indicating that its not a part of any range.

Scenario 3 If I enter the 15160. it should return the following range as 15160 to 15180

I thought of using the BETWEEN keyword But No luck with that

I am using the SQL server database, can anyone help me with the query

Upvotes: 1

Views: 218

Answers (2)

Bohemian
Bohemian

Reputation: 425448

select
    name,
    fromRange,
    toRange,
    12514 between fromRange and toRange as is_in_range 
from mean_data

is_in_range will be 1 if the input is in range, 0 otherwise

Upvotes: 0

Andrey Gurinov
Andrey Gurinov

Reputation: 2895

Something like that?

DECLARE @input INT
SELECT @input = 15160

SELECT * 
FROM your_table
WHERE
    input BETWEEN fromRange AND toRange

Upvotes: 1

Related Questions