Need to retrieve values that falls in between two columns










0















A table has columns Low and High. These columns contains range. Now I want to retrieve records when given value is present in between these columns:



Low High
0450 0459
0760 0765
0460 0467
0360 0369


I want to retrieve records where '0464', '0365', '0466' is present in between low and high.



I have got to retrieve if it is one record
As



select * 
from tablename
where '0465' between low and high


But I want the same for multiple values










share|improve this question



















  • 1





    What is data type of the Low and High columns?

    – Alexander
    Oct 29 '18 at 7:35






  • 2





    What is your expected output from that query, and if not already correct, what are you actually getting? Note that if you want to use the low and high columns as numbers, then you might want to consider making those columns some sort of numeric type.

    – Tim Biegeleisen
    Oct 29 '18 at 7:35







  • 1





    You could use OR operator in WHERE clause.

    – Alexander
    Oct 29 '18 at 7:38






  • 2





    Why are you using strings when it's obviously numbers you are dealing with?

    – Thorsten Kettner
    Oct 29 '18 at 7:39






  • 3





    Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ...

    – a_horse_with_no_name
    Oct 29 '18 at 7:47















0















A table has columns Low and High. These columns contains range. Now I want to retrieve records when given value is present in between these columns:



Low High
0450 0459
0760 0765
0460 0467
0360 0369


I want to retrieve records where '0464', '0365', '0466' is present in between low and high.



I have got to retrieve if it is one record
As



select * 
from tablename
where '0465' between low and high


But I want the same for multiple values










share|improve this question



















  • 1





    What is data type of the Low and High columns?

    – Alexander
    Oct 29 '18 at 7:35






  • 2





    What is your expected output from that query, and if not already correct, what are you actually getting? Note that if you want to use the low and high columns as numbers, then you might want to consider making those columns some sort of numeric type.

    – Tim Biegeleisen
    Oct 29 '18 at 7:35







  • 1





    You could use OR operator in WHERE clause.

    – Alexander
    Oct 29 '18 at 7:38






  • 2





    Why are you using strings when it's obviously numbers you are dealing with?

    – Thorsten Kettner
    Oct 29 '18 at 7:39






  • 3





    Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ...

    – a_horse_with_no_name
    Oct 29 '18 at 7:47













0












0








0








A table has columns Low and High. These columns contains range. Now I want to retrieve records when given value is present in between these columns:



Low High
0450 0459
0760 0765
0460 0467
0360 0369


I want to retrieve records where '0464', '0365', '0466' is present in between low and high.



I have got to retrieve if it is one record
As



select * 
from tablename
where '0465' between low and high


But I want the same for multiple values










share|improve this question
















A table has columns Low and High. These columns contains range. Now I want to retrieve records when given value is present in between these columns:



Low High
0450 0459
0760 0765
0460 0467
0360 0369


I want to retrieve records where '0464', '0365', '0466' is present in between low and high.



I have got to retrieve if it is one record
As



select * 
from tablename
where '0465' between low and high


But I want the same for multiple values







sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 29 '18 at 7:46









a_horse_with_no_name

298k46455548




298k46455548










asked Oct 29 '18 at 7:28









sufala gsufala g

1




1







  • 1





    What is data type of the Low and High columns?

    – Alexander
    Oct 29 '18 at 7:35






  • 2





    What is your expected output from that query, and if not already correct, what are you actually getting? Note that if you want to use the low and high columns as numbers, then you might want to consider making those columns some sort of numeric type.

    – Tim Biegeleisen
    Oct 29 '18 at 7:35







  • 1





    You could use OR operator in WHERE clause.

    – Alexander
    Oct 29 '18 at 7:38






  • 2





    Why are you using strings when it's obviously numbers you are dealing with?

    – Thorsten Kettner
    Oct 29 '18 at 7:39






  • 3





    Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ...

    – a_horse_with_no_name
    Oct 29 '18 at 7:47












  • 1





    What is data type of the Low and High columns?

    – Alexander
    Oct 29 '18 at 7:35






  • 2





    What is your expected output from that query, and if not already correct, what are you actually getting? Note that if you want to use the low and high columns as numbers, then you might want to consider making those columns some sort of numeric type.

    – Tim Biegeleisen
    Oct 29 '18 at 7:35







  • 1





    You could use OR operator in WHERE clause.

    – Alexander
    Oct 29 '18 at 7:38






  • 2





    Why are you using strings when it's obviously numbers you are dealing with?

    – Thorsten Kettner
    Oct 29 '18 at 7:39






  • 3





    Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ...

    – a_horse_with_no_name
    Oct 29 '18 at 7:47







1




1





What is data type of the Low and High columns?

– Alexander
Oct 29 '18 at 7:35





What is data type of the Low and High columns?

– Alexander
Oct 29 '18 at 7:35




2




2





What is your expected output from that query, and if not already correct, what are you actually getting? Note that if you want to use the low and high columns as numbers, then you might want to consider making those columns some sort of numeric type.

– Tim Biegeleisen
Oct 29 '18 at 7:35






What is your expected output from that query, and if not already correct, what are you actually getting? Note that if you want to use the low and high columns as numbers, then you might want to consider making those columns some sort of numeric type.

– Tim Biegeleisen
Oct 29 '18 at 7:35





1




1





You could use OR operator in WHERE clause.

– Alexander
Oct 29 '18 at 7:38





You could use OR operator in WHERE clause.

– Alexander
Oct 29 '18 at 7:38




2




2





Why are you using strings when it's obviously numbers you are dealing with?

– Thorsten Kettner
Oct 29 '18 at 7:39





Why are you using strings when it's obviously numbers you are dealing with?

– Thorsten Kettner
Oct 29 '18 at 7:39




3




3





Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ...

– a_horse_with_no_name
Oct 29 '18 at 7:47





Which DBMS product are you using? "SQL" is just a query language, not the name of a specific database product. Please add a tag for the database product you are using postgresql, oracle, sql-server, db2, ...

– a_horse_with_no_name
Oct 29 '18 at 7:47












1 Answer
1






active

oldest

votes


















1














OR operator



You could use OR operator in WHERE clause to concatenate multiple conditions. For example:



SELECT * 
FROM Ranges
WHERE (0464 BETWEEN low AND high) OR (0365 BETWEEN low AND high) OR (0466 BETWEEN low AND high);


Also it is possible to define custom SQL-function that will concatenate predicates via OR operator. The function will allow you to simplify the query syntax. To define custom function see documentation for your database management system.



CROSS JOIN



Another way is to build all combinations of ranges and filter values. Suppose, there are two tables in your database. Table Ranges defines low and high boundes of ranges, and table Filter contains set of values that using for ranges filtering. So, the following query returns all ranges that include values from the Filter table:



SELECT low, high
FROM Ranges, Filter
WHERE Filter.val BETWEEN Ranges.low AND Ranges.high;


Note, the query result contains duplicates of ranges. If you need to get unique ranges then use GROUP BY clause or DISTINCT operator.



A temporary table, a subquery or a collection could be used instead of Filter table.



See also live example.






share|improve this answer

























  • Yes i can use OR but when we have some 100's of values it will be difficult

    – sufala g
    Nov 12 '18 at 12:56











  • @sufalag , then you need to define custom SQL-function. If you specify your database engine, then I can provide link to details.

    – Alexander
    Nov 12 '18 at 13:00











  • It is Oracle.Thankyou

    – sufala g
    Nov 12 '18 at 13:05











  • @sufalag , see User-Defined Functions documentation.

    – Alexander
    Nov 12 '18 at 14:30











  • Thankyou for the document

    – sufala g
    Nov 12 '18 at 18:17










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53040688%2fneed-to-retrieve-values-that-falls-in-between-two-columns%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














OR operator



You could use OR operator in WHERE clause to concatenate multiple conditions. For example:



SELECT * 
FROM Ranges
WHERE (0464 BETWEEN low AND high) OR (0365 BETWEEN low AND high) OR (0466 BETWEEN low AND high);


Also it is possible to define custom SQL-function that will concatenate predicates via OR operator. The function will allow you to simplify the query syntax. To define custom function see documentation for your database management system.



CROSS JOIN



Another way is to build all combinations of ranges and filter values. Suppose, there are two tables in your database. Table Ranges defines low and high boundes of ranges, and table Filter contains set of values that using for ranges filtering. So, the following query returns all ranges that include values from the Filter table:



SELECT low, high
FROM Ranges, Filter
WHERE Filter.val BETWEEN Ranges.low AND Ranges.high;


Note, the query result contains duplicates of ranges. If you need to get unique ranges then use GROUP BY clause or DISTINCT operator.



A temporary table, a subquery or a collection could be used instead of Filter table.



See also live example.






share|improve this answer

























  • Yes i can use OR but when we have some 100's of values it will be difficult

    – sufala g
    Nov 12 '18 at 12:56











  • @sufalag , then you need to define custom SQL-function. If you specify your database engine, then I can provide link to details.

    – Alexander
    Nov 12 '18 at 13:00











  • It is Oracle.Thankyou

    – sufala g
    Nov 12 '18 at 13:05











  • @sufalag , see User-Defined Functions documentation.

    – Alexander
    Nov 12 '18 at 14:30











  • Thankyou for the document

    – sufala g
    Nov 12 '18 at 18:17















1














OR operator



You could use OR operator in WHERE clause to concatenate multiple conditions. For example:



SELECT * 
FROM Ranges
WHERE (0464 BETWEEN low AND high) OR (0365 BETWEEN low AND high) OR (0466 BETWEEN low AND high);


Also it is possible to define custom SQL-function that will concatenate predicates via OR operator. The function will allow you to simplify the query syntax. To define custom function see documentation for your database management system.



CROSS JOIN



Another way is to build all combinations of ranges and filter values. Suppose, there are two tables in your database. Table Ranges defines low and high boundes of ranges, and table Filter contains set of values that using for ranges filtering. So, the following query returns all ranges that include values from the Filter table:



SELECT low, high
FROM Ranges, Filter
WHERE Filter.val BETWEEN Ranges.low AND Ranges.high;


Note, the query result contains duplicates of ranges. If you need to get unique ranges then use GROUP BY clause or DISTINCT operator.



A temporary table, a subquery or a collection could be used instead of Filter table.



See also live example.






share|improve this answer

























  • Yes i can use OR but when we have some 100's of values it will be difficult

    – sufala g
    Nov 12 '18 at 12:56











  • @sufalag , then you need to define custom SQL-function. If you specify your database engine, then I can provide link to details.

    – Alexander
    Nov 12 '18 at 13:00











  • It is Oracle.Thankyou

    – sufala g
    Nov 12 '18 at 13:05











  • @sufalag , see User-Defined Functions documentation.

    – Alexander
    Nov 12 '18 at 14:30











  • Thankyou for the document

    – sufala g
    Nov 12 '18 at 18:17













1












1








1







OR operator



You could use OR operator in WHERE clause to concatenate multiple conditions. For example:



SELECT * 
FROM Ranges
WHERE (0464 BETWEEN low AND high) OR (0365 BETWEEN low AND high) OR (0466 BETWEEN low AND high);


Also it is possible to define custom SQL-function that will concatenate predicates via OR operator. The function will allow you to simplify the query syntax. To define custom function see documentation for your database management system.



CROSS JOIN



Another way is to build all combinations of ranges and filter values. Suppose, there are two tables in your database. Table Ranges defines low and high boundes of ranges, and table Filter contains set of values that using for ranges filtering. So, the following query returns all ranges that include values from the Filter table:



SELECT low, high
FROM Ranges, Filter
WHERE Filter.val BETWEEN Ranges.low AND Ranges.high;


Note, the query result contains duplicates of ranges. If you need to get unique ranges then use GROUP BY clause or DISTINCT operator.



A temporary table, a subquery or a collection could be used instead of Filter table.



See also live example.






share|improve this answer















OR operator



You could use OR operator in WHERE clause to concatenate multiple conditions. For example:



SELECT * 
FROM Ranges
WHERE (0464 BETWEEN low AND high) OR (0365 BETWEEN low AND high) OR (0466 BETWEEN low AND high);


Also it is possible to define custom SQL-function that will concatenate predicates via OR operator. The function will allow you to simplify the query syntax. To define custom function see documentation for your database management system.



CROSS JOIN



Another way is to build all combinations of ranges and filter values. Suppose, there are two tables in your database. Table Ranges defines low and high boundes of ranges, and table Filter contains set of values that using for ranges filtering. So, the following query returns all ranges that include values from the Filter table:



SELECT low, high
FROM Ranges, Filter
WHERE Filter.val BETWEEN Ranges.low AND Ranges.high;


Note, the query result contains duplicates of ranges. If you need to get unique ranges then use GROUP BY clause or DISTINCT operator.



A temporary table, a subquery or a collection could be used instead of Filter table.



See also live example.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 15:51

























answered Oct 29 '18 at 9:13









AlexanderAlexander

3,09371329




3,09371329












  • Yes i can use OR but when we have some 100's of values it will be difficult

    – sufala g
    Nov 12 '18 at 12:56











  • @sufalag , then you need to define custom SQL-function. If you specify your database engine, then I can provide link to details.

    – Alexander
    Nov 12 '18 at 13:00











  • It is Oracle.Thankyou

    – sufala g
    Nov 12 '18 at 13:05











  • @sufalag , see User-Defined Functions documentation.

    – Alexander
    Nov 12 '18 at 14:30











  • Thankyou for the document

    – sufala g
    Nov 12 '18 at 18:17

















  • Yes i can use OR but when we have some 100's of values it will be difficult

    – sufala g
    Nov 12 '18 at 12:56











  • @sufalag , then you need to define custom SQL-function. If you specify your database engine, then I can provide link to details.

    – Alexander
    Nov 12 '18 at 13:00











  • It is Oracle.Thankyou

    – sufala g
    Nov 12 '18 at 13:05











  • @sufalag , see User-Defined Functions documentation.

    – Alexander
    Nov 12 '18 at 14:30











  • Thankyou for the document

    – sufala g
    Nov 12 '18 at 18:17
















Yes i can use OR but when we have some 100's of values it will be difficult

– sufala g
Nov 12 '18 at 12:56





Yes i can use OR but when we have some 100's of values it will be difficult

– sufala g
Nov 12 '18 at 12:56













@sufalag , then you need to define custom SQL-function. If you specify your database engine, then I can provide link to details.

– Alexander
Nov 12 '18 at 13:00





@sufalag , then you need to define custom SQL-function. If you specify your database engine, then I can provide link to details.

– Alexander
Nov 12 '18 at 13:00













It is Oracle.Thankyou

– sufala g
Nov 12 '18 at 13:05





It is Oracle.Thankyou

– sufala g
Nov 12 '18 at 13:05













@sufalag , see User-Defined Functions documentation.

– Alexander
Nov 12 '18 at 14:30





@sufalag , see User-Defined Functions documentation.

– Alexander
Nov 12 '18 at 14:30













Thankyou for the document

– sufala g
Nov 12 '18 at 18:17





Thankyou for the document

– sufala g
Nov 12 '18 at 18:17



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53040688%2fneed-to-retrieve-values-that-falls-in-between-two-columns%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus