Search code examples
sqloracle-databasefunctionplsqlprocedure

Remove special Character from complete table


I have 5 to 6 tables in which i am inserting data using Excel file but i don't know whats the issue but for some data special character are adding and that is giving me problem. So is their any way to update complete table by passing the only Table_name in some procedure.currently i am using one function to update the column but i want something more easy to use (for my other team member). I am using below function .

create or replace FUNCTION pfm_on_varchar(
    p_str IN VARCHAR2)
  RETURN VARCHAR2
IS
  o_str   VARCHAR2(4096) := '';
  asc_val NUMBER;
BEGIN
  FOR I IN 1 .. LENGTH(p_str)
  LOOP
    asc_val := ascii(SUBSTR(p_str,i,1));
    IF ((asc_val BETWEEN 48 AND 57) OR (asc_val BETWEEN 65 AND 90) OR (asc_val BETWEEN 97 AND 122)) THEN
      o_str := o_str || chr(asc_val);
    END IF;
  END LOOP;
  RETURN o_str;
END pfm_on_varchar;

and how can I pass table name and column name in a procedure to update the table ?I am trying this code

CREATE OR REPLACE PROCEDURE removeSpecialChar(table_new IN varchar2, column_new IN varchar2)
AS
BEGIN 
  update table_new
  set column_new = PFM_ON_VARCHAR(column_new);
  end removeSpecialChar;
  /

Error I am getting

Error(4,3): PL/SQL: SQL Statement ignored
Error(4,10): PL/SQL: ORA-00942: table or view does not exist

Solution

  • update table
    set columnName = regexp_replace('This is a test. $%&^*&* ', '[^A-Za-z .?!]', '')
    

    Add all the characters which you don't want removed in the [^A-Za-z .]

    For the procedure you could do a dynamic sql. Otherwise it will not pick the variable up. Like this:

    CREATE OR REPLACE PROCEDURE removeSpecialChar(table_new IN varchar2, column_new IN varchar2)
    AS
    BEGIN 
      execute immediate 'update '||table_new||
      ' set '||column_new||' = PFM_ON_VARCHAR('||column_new||')';
      end removeSpecialChar;
    /