0

Report for clients with several conditions.
The result must contain 'upper_income_level' - the upper limit of the 'cust_income_level' column.

'cust_income_level' field has for example similar content: ‘G: 130,000 - 149,999’. How to extract only 149999 and convert to an integer?

1
  • 1
    Can you please edit your question, provide a few rows of sample data, including enough columns of data to help us understand your table, and a sample required output? Commented Jun 18, 2020 at 13:59

2 Answers 2

1

If you want the numbers at the end of the string, you can use:

select replace(regexp_substr(cust_income_level, '[0-9,]+$'), ',', '')
Sign up to request clarification or add additional context in comments.

Comments

0

Without regular expressions, you can use

substr( cust_income_level, instr( cust_income_level, '-') + 2 ) as cust_income_level

If you want to use it as numeric in an ORDER BY, you can use function TO_NUMBER(), but this is error prone since you'll get errors when the value isn't an actual number value.

to_number( substr( cust_income_level, instr( cust_income_level, '-') + 2 )) as cust_income_level

Instead, for the ORDER BY, you can add leading zero's and use that value to get the correct order. If there is a value which isn't a number, it will be presented in the results, but at least you won't get an error.

lpad( substr( cust_income_level, instr( cust_income_level, '-') + 2 ), 9, '0')

Example above will add leading zero's until the value is 9 characters long

  • '3' becomes '000000003'
  • '40' becomes '000000040'
  • etc.

Sorting on the results above will lead to a correct ordering of numbers in a varchar2 datatype.

3 Comments

Thank you! Can you tell me how to convert it to integer (because I need it in ORDER BY)
You can, but be aware that you will get errors when it is not convertable to a number, in case the value contains an alphanumeric character. Instead it is possible better to order by on the value which contains leading zeros. I did update the answer.
You’re welcome, please mark the answer as accepted if you’re satisfied. That way anyone else will see the question as answered. Thanks.

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.