2

How do I remove only trailing spaces and chr(10) and chr(13) using Oracle regexp in a sql statement?

Example:

with txt as (select chr(10)||chr(10)||'  Hey Bob   '||chr(10)||chr(13) a  from dual)
select a
      ,regexp_replace(a,chr(10)||'+|'||chr(13)||'+|'||chr(32)||'+$','')
      ,regexp_replace(a,'['||chr(10)||'+'||chr(13)||'+'||chr(32)||'+]$','')
  from txt;

Desired result:

'  Hey Bob'
1. Leading and non-trailing spaces remain
2. Trailing spaces and eol characters removed

1 Answer 1

4

It would be better to use [[:space:]] to capture all whitespace:

regexp_replace(a, '[[:space:]]+$', '')

But if you explicitly want just the new-line (10), carriage-return (13) and space (32) characters, you can do:

regexp_replace(a, '[' || chr(10) || chr(13) || ' ]+$', '')
--                                              ^-- space character.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much - this is what I needed - moving the '+' outside of the '[]'
I also tried using [:space:] instead of the explicit new-line, etc, and it works perfectly - handles tab characters as well. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.