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 8925591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:44:28+00:00 2026-06-15T07:44:28+00:00

I am working in mssql database. I have the following scenario, I have values

  • 0

I am working in mssql database. I have the following scenario,

I have values in row as

2-5,10-12,67-89….

I need a sql query to fetch only those rows which has atleast one range that contains the user input.

For ex if the user input is 4, then there should be atleast one range that contains 4 like (1-5).

Is it possible to achieve the same with single query.

  • 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-06-15T07:44:29+00:00Added an answer on June 15, 2026 at 7:44 am

    Try this:

    DECLARE @numberToFind INT = 4;
    ;WITH CTE
    AS
    (
        SELECT 
          arange,
          SUBSTRING(arange, 1, CHARINDEX('-', arange,1) - 1) "From",
          SUBSTRING(arange, 
                    CHARINDEX('-', arange,1) + 1, 
                              LEN(arange) - CHARINDEX('-', arange,1) + 1) "To"
        FROM @ranges
    ) 
    SELECT arange
    FROM CTE
    WHERE @numberToFind BETWEEN "From" AND "To";
    

    SQL Fiddle Demo

    If these ranges are stored as a single varchar string, then you have to parse these comma separated ranges first like so:

    DECLARE @ranges VARCHAR(100) = ('2-5,10-12,67-89');
    declare @numberToFind INT = 4;
    
    DECLARE @Xml xml =  CONVERT(xml,
                                '<root><s>' + 
                                REPLACE(@ranges, ',', '</s><s>') + 
                                '</s></root>');
    
    ;WITH ParsedRanges
    AS
    (
        SELECT arange = T.c.value('.','varchar(20)')
        FROM @Xml.nodes('/root/s') T(c)
    ), CTE
    AS
    (
        SELECT 
          arange,
          SUBSTRING(arange, 1, CHARINDEX('-', arange,1) - 1) "From",
          SUBSTRING(arange, 
                    CHARINDEX('-', arange,1) + 1, 
                    LEN(arange) - CHARINDEX('-', arange,1) + 1) "To"
        FROM ParsedRanges
    ) 
    SELECT arange
    FROM CTE
    WHERE @numberToFind BETWEEN "From" AND "To";
    

    SQL Fiddle Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on PHP and database MySQL. I have two tables in SQL
I have a java program that connects to a MySql database and it's working
My team working in a project using Winforms application(c#) & MSSQL 2005 as database.
I am working on porting over a database from a custom MSSQL CMS to
I am working with a rather large mysql database (several million rows) with a
I'm working on an ASP (Classic) application using MS SQL as database. In the
I have created a database on SQL server and a front end user application
I have the following code for uploading a csv file to a mysql database.
I am working on python and tried to fetch some data from Mysql Database
I am working in a MySQL database. I have added a custom field to

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.