Search code examples
plsqloracle-apex

How to remove error code and print only message | PLSQL


I have to print only error message and remove code number

BEGIN

IF :MY_TEXT is null THEN 
   raise_application_error(-20001,'Text field cannot be empty')
END IF;

Exception 
     when others then
             :MSG := replace(SQLERRM,'^ORA-+:','');

 
END;

Expected output :

Text filed cannot be empty

Solution

  • You need to use something like this SUBSTR(MSG, INSTR(MSG, ':', 1, 1)+2 )

    DECLARE
    MY_TEXT VARCHAR2(100);
    MSG VARCHAR2(100);
    
    BEGIN
    
    IF MY_TEXT is null THEN 
       raise_application_error(-20001,'Text field cannot be empty');
    END IF;
    
    Exception 
         when others then
                 MSG := SQLERRM ;
                 dbms_output.put_line( SUBSTR(MSG, INSTR(MSG, ':', 1, 1)+2 ) );
     
    END;
    /