Remove unwanted line feeds in your data with Oracle SQL

To remove these line feeds that you do not need, you will need to make use of the chr() function built-in to Oracle, which allows you to find special ASCII characters. Consider the case that a product’s description may contain line feeds such as the following:

This is an amazing product.
It removes stains from your clothing, restoring crisp colors as if fresh from the store.
Buy it today!

select 
replace(
  replace(
    replace(
      product_description
    ,chr(10),' ') -- replace new lines with spaces
  ,chr(13),' ') -- replace carriage returns with spaces
,'  ',' ') as product_description -- cleans up double-spaces
from products
where product_id=1;

You should see that the line feeds are removed in the end result.

This is an amazing product. It removes stains from your clothing, restoring crisp colors as if fresh from the store. Buy it today!

Note that there is a replace() function that changes double-spaces to single-spaces. This may be needed because we may run into the case where there might be a trailing space at the end of a line, immediately before a line feed, which will become a double-space and may mess up your spacing slightly.

Leave a Reply

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