Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6923125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:29:43+00:00 2026-05-27T10:29:43+00:00

Using SQL Server 2005 Given a table: | Role | Low | High |

  • 0

Using SQL Server 2005

Given a table:

| Role  | Low  | High |
| A     | AA00 | ZZ*  |
| B     | *    |      |
| C     | AA00 | AZ99 |

How would I go about performing a query that selects rows where a value falls within the LOW – HIGH range.

e.g. I would like to select Roles A and B if I am comparing Low and High to ‘BB’ and role B if I am comparing the range to ’22’

AB99 should return A, B, C

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:29:44+00:00Added an answer on May 27, 2026 at 10:29 am

    Try this:

    DECLARE @CompVal VarChar(100) = '22';
    
    WITH T AS (
      SELECT Role, Low, High
      FROM (
        SELECT 'A' Role, 'AA00' Low, 'ZZ*' High
        UNION
        SELECT 'B' Role, '*' Low, NULL High
        UNION
        SELECT 'C' Role, 'AA00' Low, 'AZ99' High
      ) U
    )
    SELECT *
    FROM T
    WHERE @CompVal BETWEEN Low AND High
      OR (@CompVal >= Low AND NULLIF(High, '*') IS NULL)
      OR (NULLIF(Low, '*') IS NULL AND @CompVal <= High)
    ;
    

    I’m assuming that both your * and NULL values are used like wildcard values. This produces proper results for each of your example cases.

    Note that this also depends on which collation you’re using. For most (including the default collation SQL_Latin1_General_CP1_CI_AS) it will be just fine.

    EDIT: Looking more closely you may want to replace the WHERE clause with this:

    WHERE @CompVal BETWEEN
          REPLACE(ISNULL(Low, '*'), '*', '0000')
      AND REPLACE(ISNULL(High, '*'), '*', 'ZZZZ')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the constraint of only using T-Sql in Sql Server 2005, is there a
[using SQL Server 2005] I have a table full of users, I want to
Using SQL Server 2005 and 2008. I've got a potentially very large table (potentially
Using SQL Server 2005 I want to generate the table values between the two
I'm using SQL Server 2005 and am logged in as sa . I would
I am using SQL SERVER 2005. I have a table table1(ID,col1,col2,col3,col4); Now I have
I am using SQL Server 2005. I have a table called Product. Now I
using SQL Server 2005 standard edition with SP2 I need to design a table
I am using MS SQL server 2005 I have a table with 3 columns
Using SQL Server 2005 and Management Studio how do I insert a picture into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.