Skip to Main Content
  • Questions
  • Remove a part of string between special characters and display the remaining portion

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Raghu.

Asked: May 15, 2020 - 11:28 am UTC

Last updated: May 19, 2020 - 9:31 am UTC

Version: 18.0.0.0.0

Viewed 10K+ times! This question is

You Asked

CREATE TABLE test(
    message VARCHAR2(500) 
);


INSERT INTO test VALUES ('successfully sent. count:3, no:125');
INSERT INTO test VALUES ('Action done. count:523, no:789456');


select * from test


MESSAGE
----------------------------------
successfully sent. count:3, no:125
Action done. count:523, no:789456

from the above table i would like to get the output as below (remove the data after first period(0) and before first comma(,))

MESSAGE
----------------------------------
successfully sent. no:125
Action done. no:789456

Another question based on an extension to the above question

If we have another row as below in the same table

INSERT INTO test VALUES ('Good sent. no:1254985');


select * from test 


MESSAGE
----------------------------------
successfully sent. count:3, no:125
Action done. count:523, no:789456
Good sent. no:1254985

And i would like to get output as
MESSAGE
----------------------------------
successfully sent. no:125
Action done. no:789456
Good sent. no:1254985 -- Note that for the first two records, count is trimmed off and the third one comes as is because it doesn't have the string (count:xx)part.

Thanks in advance




and Chris said...

There are many ways you could do this. Broadly there are two methods:

- Use a regular expression to remove the unwanted characters
- Substr the values to preserve the parts you want to keep

Here's two implementations of these:

select regexp_replace ( message, '\..*,', '.' ) regex,
       substr ( message, 1, instr ( message, '.' ) ) || 
         substr ( message, instr ( message, ',' ) + 1 ) subin
from   test;

REGEX                        SUBIN                       
successfully sent. no:125    successfully sent. no:125    
Action done. no:789456       Action done. no:789456 


The regex finds all the characters between period and comma
Substr returns the characters up to the first period. Then concatenates all the characters after the comma

Whichever one you choose, ensure you test thoroughly! It's often the case there are some strings that break these rules. e.g. having extra periods or commas.

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

on few more cases

Rajeshwaran, Jeyabal, May 18, 2020 - 3:12 pm UTC

adding this insert -as per the given requirement

INSERT INTO test VALUES ('Good sent. no:1254985');


substr - part doesn't work perfect.

demo@PDB1> select regexp_replace ( message, '\..*,', '.' ) regex,
  2         substr ( message, 1, instr ( message, '.' ) ) ||
  3           substr ( message, instr ( message, ',' ) + 1 ) subin
  4  from   test;

REGEX                          SUBIN
------------------------------ ----------------------------------------
successfully sent. no:125      successfully sent. no:125
Action done. no:789456         Action done. no:789456
Good sent. no:1254985          Good sent.Good sent. no:1254985


given the pattern "count:" is going to be the third word in the input string - we can have few more options in Regex like this:

demo@PDB1> select message ,
  2      regexp_replace( message, '(\S+) (\S+) (count:\d+,) (\S+)','\1 \2 \4') replaced_txt,
  3      regexp_replace( message, '(\S+) (\S+) (\S+) (\S+)','\1 \2 \4') replaced_txt2
  4  from test
  5  /

MESSAGE                                  REPLACED_TXT                   REPLACED_TXT2
---------------------------------------- ------------------------------ ------------------------------
successfully sent. count:3, no:125       successfully sent. no:125      successfully sent. no:125
Action done. count:523, no:789456        Action done. no:789456         Action done. no:789456
Good sent. no:1254985                    Good sent. no:1254985          Good sent. no:1254985

Chris Saxon
May 19, 2020 - 9:31 am UTC

Good catch

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.