Oracle – remove unwanted spaces in string in oracle

oracle

I have a query on SQL in Oracle.

I have a text in one of free-text column of an Oracle table.

The value is:

"This is a  test   value"

I want to change this to more appropriate which is

"This is a test value"`

I have used REGEX_REPLACE function however it removed all the spaces. As mentioned I do want the correct space to be displayed in the text but want to remove the unwanted.

Please can anybody help me out in this?

Thanks
Yash

Best Answer

This is how I would approach it:

select
  regexp_replace ( 'This is a  test   value', '\s+', ' ')
from dual

Bear in mind this will also replace all multiple whitespaces, such as tabs, as well as spaces. If you really just want spaces:

select
  regexp_replace ( 'This is a  test   value', ' +', ' ')
from dual