Correct syntax for SQL between-like query

Posted on 2009-12-16
Last Modified: 2012-05-08
I am trying to structure a query in a stored proc to handle an incoming zip code as follows: zip comes in and I need to search rule table to see if the zip value is between a value in BeginRange column and EndRange column.  So if 77400 comes in and a record exists where BeginRange column is 77890 and EndRange column is 77405, the record is found.  I can't quite see how the BETWEEN clause would work?  Can someone get me unstuck with the correct query syntax?
Question by:srbentley
    LVL 142

    Accepted Solution

    this will do:

    WHERE EndRange >= 77440
      AND BeginRange <= 77440


    WHERE 77440 BETWEEN BeginRange AND EndRange
    LVL 75

    Expert Comment

    by:Aneesh Retnakaran
    WHERE @ZipValue BETWEEN @BeginRange and @EndRange

    Author Closing Comment

    I knew that BETWEEN could work with params, but I didn't know you could reverse things  and use it with the column names.  Thanks.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Suggested Solutions

    PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    746 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