Skip to Main Content
  • Questions
  • how to replace the line break with tab space in database in all the column of the database

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, jayaraj.

Asked: April 04, 2017 - 8:22 pm UTC

Last updated: April 07, 2017 - 2:40 am UTC

Version: oracle 11g

Viewed 10K+ times! This question is

You Asked

We have one database which has 10 table contains 10 columns in each table, which contain Line break data(\n) in 5 Columns I Want all the 5 different columns data to be replaced with spaces if it contains line break. I want it in a single query at schema level

and Connor said...

update MY_TABLE
set 
  col1 = replace(col1,chr(13),' '),
  col2 = replace(col2,chr(13),' '),
  col3 = replace(col3,chr(13),' '),
  col4 = replace(col4,chr(13),' '),
  ...
  ...
where
  instr(col1,chr(13)) > 0 or
  instr(col2,chr(13)) > 0 or
  instr(col3,chr(13)) > 0 or
  instr(col4,chr(13)) > 0 or
  ...
  ...


Depending on your environment, this might be chr(10), or chr(13) or both.

Rating

  (1 rating)

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

Comments

i need a generic query

jayaraj, April 05, 2017 - 3:57 am UTC

I need a generic Query without table name and column based on system tables.
Connor McDonald
April 07, 2017 - 2:40 am UTC

You can build up the same query just be querying user_tab_cols

SQL> variable table_name varchar2(30)
SQL> exec :table_name := 'EMP';

PL/SQL procedure successfully completed.

SQL>
SQL> col sortkey1 nopri
SQL> col sortkey2 nopri
SQL>
SQL> select 1 sortkey1, 0 sortkey2, 'update '||:table_name||' set ' from dual
  2  union all
  3  select 2 , column_id, decode(column_id,1,' ',',')||column_name||' = replace('||column_name||',chr(13),'' ''),'
  4  from user_tab_cols
  5  where table_name = :table_name
  6  union all
  7  select 3 , column_id, decode(column_id,1,'where ',' or ')||'instr('||column_name||',chr(13))>0'
  8  from user_tab_cols
  9  where table_name = :table_name
 10  order by 1,2;

'UPDATE'||:TABLE_NAME||'SET'
------------------------------------------------------------------------------------------------------------------------
update EMP set
 EMPNO = replace(EMPNO,chr(13),' '),
,ENAME = replace(ENAME,chr(13),' '),
,JOB = replace(JOB,chr(13),' '),
,MGR = replace(MGR,chr(13),' '),
,HIREDATE = replace(HIREDATE,chr(13),' '),
,SAL = replace(SAL,chr(13),' '),
,COMM = replace(COMM,chr(13),' '),
,DEPTNO = replace(DEPTNO,chr(13),' '),
where instr(EMPNO,chr(13))>0
 or instr(ENAME,chr(13))>0
 or instr(JOB,chr(13))>0
 or instr(MGR,chr(13))>0
 or instr(HIREDATE,chr(13))>0
 or instr(SAL,chr(13))>0
 or instr(COMM,chr(13))>0
 or instr(DEPTNO,chr(13))>0


You can add in whatever logic you want to exclude numerics, dates etc.