2

I got some sql script below to remove word between words, but it will find last occurrence of my (/hide) instead of the first occurrence. Need help to get the output as expected. Thanks.

select regexp_replace('(hide)it(/hide)should be show(hide)my(/hide) text',
       '^\(hide\).*\(/hide\)', '') "TESTING" 
  from dual;

I expect the output will be:

should be show text

but the actual output is:

text

If my data is in one of the column with datatype of clob. As currently i use below script to select. For example my table is testing_table with column of desc_str with data_type of clob which inside contain value '(hide)it(/hide)should be show(hide)my(/hide) text';

select trim(to_char(regexp_replace(desc_str,'^\(hide\).*\(/hide\)',''))) as desc 
  from testing_table 
 where OOE_FP_SS_ID = $id;
1
  • I am using the oracle sql developer. Commented Oct 25, 2019 at 9:44

1 Answer 1

3

Based on your example:

SQL> with test (col) as
  2    (select '(hide)it(/hide)should be show(hide)my(/hide) text' from dual)
  3  select regexp_replace(col, '\(hide\)\w+\(/hide\)', '') result
  4  from test;

RESULT
-------------------
should be show text

SQL>

[EDIT: CLOB]

You asked for a CLOB. Here's an example:

SQL> create table test (desc_str clob);

Table created.

SQL> insert into test values ('(hide)it(/hide)should be show(hide)my(/hide) text');

1 row created.

SQL> select regexp_replace(desc_str, '\(hide\)\w+\(/hide\)', '') result from test;

RESULT
--------------------------------------------------------------------------------
should be show text

SQL>

Another example:

SQL> with test (desc_str) as
  2    (select '(hide)ItemId: 4334(/hide)|(hide)Description Item - SubType:(/hide) Name - Item|(irow)Price Value: MYR 1,668' from dual)
  3  select regexp_replace(desc_str, '\(hide\).+?\(/hide\)', '') result from test;
                                                ^
RESULT                                          | this question mark was missing
------------------------------------------
| Name - Item|(irow)Price Value: MYR 1,668

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

11 Comments

How about if my text '(hide)it(/hide)should be show(hide)my(/hide) text' is one of the column in table with data type of clob?
Try and see what happens.
with test (col) as (select to_char(desc_str) from testing_table where id = 6041) select regexp_replace(col, '(hide)\w+(/hide)', '') result from test;
i try using above code it cannot hide the word between hide
I try above data is working fine, but may i know why the data below unable to hide. (hide)ItemId: 4334(/hide)|(hide)Description Item - SubType:(/hide) Name - Item|(irow)Price Value: MYR 1,668
|

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.