Ensurie a number is not within the range of existing numbers.

Posted on 2010-01-04
Last Modified: 2012-05-08
I have a table that has a "start number", an "end number"  and a user id.

I need to be able to determine whether or not that a new series of numbers are within any of the ranges already entered into the database.

For example:
1           10        user-id
11         20        user-id

If I enter 4 to 12, I should get a false result while entering 21 to 400 would give a positive result.

Question by:bryan_z
    LVL 60

    Expert Comment

    declare @no int
    set @no  = 21
    select case when @no between startdate and enddate then 'exists' else 'not' end
    from tablename
    LVL 13

    Accepted Solution

    Use the following code:
    declare @startno int;
    declare @endno int;
    declare @userid varchar(8);
    create table userrange(startno int, endno int,userid varchar(8));
    set @startno = 21;
    set @endno = 400;
    set @userid = 'a';
    select case when count(startno) = 0 then 'negative' else 'positive' end as resulttext
    from userrange 
    where userid = @userid
    and @startno not between startno and endno
    and @endno not between startno and endno;

    Open in new window

    LVL 40

    Expert Comment

    Is there any gam between start and end numbers for different users as mentioned like this.

    1           3        user-id
    13         20        user-id

    If yes and you enter 4 to 12, then what would be the result you are expecting?
    LVL 40

    Expert Comment

    *gam = gap
    LVL 13

    Expert Comment

    I think, it should return positive. Because there are no rows that overlap with the given interval.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
    Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    794 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

    16 Experts available now in Live!

    Get 1:1 Help Now