2

I want to use the Oracle REGEXP_REPLACE to remove some dots from a String. I have to remove from the second dot if exists. For example:

4       => 4
5       => 5
5.1     => 5.1
5.1.1   => 5.11
5.1.2   => 5.12
5.1.2.1 => 5.121
5.1.2.2 => 5.122
6       => 6

I have this

select REGEXP_REPLACE(num, '(\d+)(\.)(\d+)(\.)(\d+)', '\1.\3\5')
from my_table;

the problem is that my query is made just for two dots. If the string has more than two dots, I have to modify the query to accept more dots and so. Is there any way to do this automatically?

Thanks

2 Answers 2

2

A posible "dirty" solution

select REGEXP_REPLACE(num, '(\d+)(\.)(.*)', '\1\2') || 
       REPLACE( REGEXP_REPLACE(num, '(\d+)(.*)', '\2') ,'.','') 
from my_table;

and although it's not at all clear, it works.

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

Comments

1
SELECT SUBSTR(mystr, 1, INSTR(mystr,'.'))
       || REPLACE(SUBSTR(mystr, INSTR(mystr,'.')+1),'.')
FROM my_table;

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.