skip to Main Content

I am trying to replace multiple whitespaces from a column in Redshift(Postgres)

Query used :

Select replace(col_name,'  ',' ') from table;

above query is not working for multiple whitespaces only.

I have tried replacing other characters in the same query, also for a single whitespace it’s working fine.

Can someone help me here?

2

Answers


  1. This will replace all occurrences of multiple whitespaces with a single whitespace:

    SELECT  REGEXP_REPLACE('a phrase   with    multiple  whitespaces', '\s+', ' ')
    ---
    
    a phrase with multiple whitespaces
    
    Login or Signup to reply.
  2. Based on the answer of @Quassnoi, this one works for me:

    SELECT  REGEXP_REPLACE('a phrase   with    multiple  whitespaces', 's+', ' ','g'); 
    

    It uses the g flag, to search for all matches. And replace these matches.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search