0

How can I get range of result using between or any other operator?

Example:

select * from sku between 'sku1' and 'sku5'

Input Data

SKU4 
SKU5 
SKU10 
SKU11 
SKU12 
SKU1 
SKU2 
SKU3 
SKU150101 
SKU15010601 
SKU4 
SKU1 
SKU2 
SKU1 
SKU2 
SKU3 
SKU20164 
SKU20165 
SKU20166 
SKU20167 
SKU20168 
SKU20169

Result expected:

SKU1
SKU2
SKU3
SKU4
SKU5

The sku is not fix to the format sku[1-...]. It also can be any other string that been set to it.

10
  • 1
    And what is wrong with your expression? It seems to do what you want. Commented Jul 10, 2017 at 4:16
  • i got only sku1 and sku5 Commented Jul 10, 2017 at 4:19
  • What is the actual data in your table? Do sku2, sku3 and sku4 exist? Commented Jul 10, 2017 at 4:20
  • yes..sku2,sku3,sku4 all exist in the table Commented Jul 10, 2017 at 4:21
  • 1
    Please append missed information in your question, not in comments. Commented Jul 10, 2017 at 9:32

4 Answers 4

3

The problem is that your numbers are strings and the sort order for strings is different then those for numbers. If every sku starts with the string sku it would have been a better design to not store that prefix at all and store the SKU number as a proper number in the database.


You can use something like this:

select *
from the_table
where sku like 'sku%' -- only those that start with the prefix
  and to_number(regexp_replace(sku, '[^0-9]', '')) between 1 and 5

The regexp_replace() extracts only digits from the string, then converts that to a proper number which can be compared correctly using the between operator.

Sign up to request clarification or add additional context in comments.

1 Comment

tq @a_horse_with_no_name ..but this code seems like i know what are the front of it..example if it start with 2017MM0001? the sku1-sku5 is simple example..it can be 2017MM0001-2017MM0005 or abcd12345abcd1-abcd12345abcd5 or abcd-abce..
0

If the below sample is your data as you said earlier:
here are the example data i got: SKU4 SKU5 SKU10 SKU11 SKU12 SKU1 SKU2 SKU3 SKU150101 SKU15010601 SKU4 SKU1 SKU2 SKU1 SKU2 SKU3 SKU20164 SKU20165 SKU20166 SKU20167 SKU20168 SKU20169..i want only sku1,sku2,sku3,sku4,sku5

Then try below query:

select * 
from (
  Select * 
  from table_name 
  order by column_name aesc
) 
fetch first 5 rows only;

1 Comment

hi @Rajesh..i cannot only fetch 5 rows only because maybe there still a lot more rows to fetch..
0

For string operation you want to use exact scenario with text case sensitive. Can you try the below code

Method 1:

Select * from table_name where column_name like 'sku_'

Method 2:

Select * from table_name where column_name in ('sku1','sku2','sku3','sku4','sku5')

7 Comments

already try..still not get expected result..sku1sfsdfs, sku2sdfsdfs, sku3asdada, sku4dgdgd, sku5ffsfs still showing..
select substr(column_name, 1, 4) where lower(column_name) like 'sku%';
@eikaz code is updated. Check the updated code and let me know the result.
hi @sahathulla..if use like then all the string contains sku1 will appear right? hi g00dy.. i cannot use substr because the format for it does not fix to sku[1- ...]
@eikaz this code will show only sku[1-9]. if you have sku1 twice it shows both the code. if you have sku11saece it will not show.
|
0

Can't you just get the numbers from the string and use it in the BETWEEN clause ? Considering it always starts with 3 letters :

Ex :

CREATE TABLE TEST (
  ID NUMBER PRIMARY KEY,
  TEXT VARCHAR(255)
);

INSERT INTO TEST(ID, TEXT) VALUES(1, 'sku1');
INSERT INTO TEST(ID, TEXT) VALUES(2, 'sku2');
INSERT INTO TEST(ID, TEXT) VALUES(3, 'sku3');
INSERT INTO TEST(ID, TEXT) VALUES(4, 'sku4');
INSERT INTO TEST(ID, TEXT) VALUES(5, 'sku23');
INSERT INTO TEST(ID, TEXT) VALUES(6, 'sku40');
INSERT INTO TEST(ID, TEXT) VALUES(7, 'sku123');

Then :

SELECT *
FROM TEST
WHERE TO_NUMBER(SUBSTR(TEXT, 4)) BETWEEN 3 AND 30;

Result :

|--------------|--------------|
|      ID      |     TEXT     |
|--------------|--------------|
|      3       |     sku3     |
|      4       |     sku4     |
|      5       |     sku23    |
|--------------|--------------|

Or of course if you need the strings in the BETWEEN clause :

SELECT *
FROM TEST
WHERE TO_NUMBER(SUBSTR(TEXT, 4)) BETWEEN TO_NUMBER(SUBSTR('sku3', 4)) AND TO_NUMBER(SUBSTR('sku30', 4))

Try the fiddle.

EDIT : According to the comment, if you need to get the last digits from the string, you can use the following (uses REGEXP_SUBSTR function and a regexp to extract the digits from the end of the string) :

SELECT *
FROM TEST
WHERE TO_NUMBER(REGEXP_SUBSTR(TEXT,'\d+$')) BETWEEN 3 AND 30;

\d+ matches a group of digits. $ matches end of line.

New fiddle.

4 Comments

hi @yann39 ..if i know what are the character will start it will be simple.. but the parameter is dynamic...it can be 2017MM0001-2017MM0005 or abcd12345abcd1-abcd12345abcd5 or abcd-abce..
OK so does it concerns only the last digit characters from the string ? Then could you consider using a regexp to get them independently from the string ? See my edited answer.
TEST = table name, TEXT = column name?
Yes you have to replace with your own values :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.