Oracle regular expressions

A recent question on CDOS was regarding how to have only one space at the end of a CSV output row. Below was my response…

SQL> create table t1110(c varchar2(10))
  2  /
 

Table created.
 

SQL> insert into t1110 values('1')
  2  /
 

1 row created.
 

SQL> insert into t1110 values('2    ')
  2  /
 

1 row created.
 

SQL> insert into t1110 values('3 new  ')
  2  /
 

1 row created.
 

SQL> column len format 9
SQL> column orig format a10
SQL> column newone format a10
SQL> column new_len format 9
SQL> set trims off
SQL> select length(c) len,
  2         c orig,
  3         regexp_replace(c, '\W+$', ' ') newone,
  4         length(regexp_replace(c, '\W+$', ' ')) new_len
  5     from t1110
  6  /
 

LEN ORIG       NEWONE     NEW_LEN
--- ---------- ---------- -------
  1 1          1                1
  5 2          2                2
  7 3 new      3 new            6

SQL>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.