PostgreSQL regexp_replace() to keep just one whitespace

SELECT trim(regexp_replace(col_name, '\s+', ' ', 'g')) as col_name FROM table_name;

Or In case of update :

UPDATE table_name SET col_name = trim(regexp_replace(col_name, '\s+', ' ', 'g'));

The regexp_replace is flags are described on this section of the documentation.

Leave a Comment