Check a number against several ranges

Posted on 2006-06-05
Last Modified: 2010-04-16

I have a database table which has three columns: From | To | Value

What I want is to check a number that I get to which range it belongs so I can return the Value of that row.

For example for table like:

0 | 100 | 30
101 | 200 | 50

if I am given the number 148 I want to return 50.

What's the easiest way of doing that?

Thank you!!
Question by:infodigger
    LVL 25

    Accepted Solution

    Assuming the from and to don't overlap for any record:

    select value from table
    where from <= @argValue and to >= @argValue
    LVL 25

    Expert Comment


    select value from table
    where @argValue between from and to

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now