0

I have a table as:

column1                         column2 
The first value is 200 gb        need to restart (2 times)
The 2nd value is 700 gb          need (optional) to restart (10 times)

I am trying to get the numeric values from the table. The expected output is

column1_numeric      column2_numeric 
200                   2
700                   10

For column1: i trying to get data using: regexp_replace(column1, '[^0-9]', '') as column1_numeric; but this is not working for the 2nd row and returns 2700

For column2: I am trying as: regexp_replace(regexp_extract(column2,'\\((.*?)\\)'), '[^0-9]', '') as column2_numeric but this is also not working for 2nd row and returns null values

Any suggestions please?

3 Answers 3

1

Extract last numeric value from the string '(\\d+)([^0-9]*)$':

select 
      regexp_extract(column1,'(\\d+)([^0-9]*)$',1) as column1_numeric,
      regexp_extract(column2,'(\\d+)([^0-9]*)$',1) as column2_numeric
   ...

It extracts

column1_numeric      column2_numeric 
200                   2
700                   10

Also instead of [^0-9] (not a digit) you can use \\D, which is a bit shorter:

'(\\d+)(\\D*)$'
Sign up to request clarification or add additional context in comments.

3 Comments

It worked! If we have a record as need (optional) to restart (10 times) for next 3 days in column 2; how to get the value 10 from it? The query now returns 3 for this situation.
Plus if there is comma in that value (for example: need (optional) to restart (1,000 times) for next 3 days -how to avoid comma and get 1000 from it?
@Tanvir select regexp_extract('need (optional) to restart (1,000 times) for next 3 days','([0-9,]+)',1)
0

If you use the below regex it will work for both the columns and extract only the number from the string.

var numberPattern = /\d+/g;
'The first value is 200 gb'.match( numberPattern ).join('') // 200

'need to restart (2 times)'.match( numberPattern ).join('') // 2

Comments

0

Please try this
select REGEXP_REPLACE('The first value is 200 gb','[^0-9]','')
I got result as 200
Try this :
select REGEXP_REPLACE(substring('The 2nd value is 200 gb',-6),'[^0-9]','')

Comments

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.