Home > Software design >  ORACLE SQL | Escape a whole string variable
ORACLE SQL | Escape a whole string variable

Time:11-15

I have a variable that has an open and close parentheses inside, but it reads by the regexp_like as a regular expression thus invalidating my code below. my question is how can we escape the whole string variable without modifying the string itself

declare 
    TW_keyword varchar2(100) := 'THE QUICK (BROWN)';
    tw_result  varchar2(100);
begin 
    SELECT CASE
    WHEN regexp_like ('IS THE QUICK (BROWN) FOW', '(^|\W)'|| TW_keyword ||'($|\W)')
    THEN 'found'
    ELSE 'not found'
    END rl INTO tw_result 
    FROM DUAL;
    
    dbms_output.put_line(tw_result);
end;

RESULT:

PL/SQL procedure successfully completed.
not found

CodePudding user response:

If its just brackets you expect, then something like this should do

SQL> declare
  2      TW_keyword varchar2(100) := 'THE QUICK (BROWN)';
  3      tw_result  varchar2(100);
  4  begin
  5      tw_result := CASE
  6      WHEN regexp_like ('IS THE QUICK (BROWN) FOW', '(^|\W)'|| replace(replace(TW_keyword,'(','\('),')','\)') ||'($|\W)')
  7      THEN 'found'
  8      ELSE 'not found'
  9      END;
 10
 11      dbms_output.put_line(tw_result);
 12  end;
 13  /
found

PL/SQL procedure successfully completed.

Note that you also don't have to select-from-dual just to do an assignment

  • Related