Monday, September 21, 2020

SQL: Replace repeated spaces with a single space

In SQL dialects that provide a REPLACE function you can replace instances of repeated spaces with a single space (you may replace CHAR(7) with another character that you know won't appear in your dataset):
1
SELECT REPLACE(REPLACE(REPLACE('some   string    with         many     spaces', '  ', ' ' + CHAR(7)), CHAR(7) + ' ', ''), ' ' + CHAR(7), ' ');

No comments:

Post a Comment