Hello, I need to remove duplicate spaces in a string and leave only 1, identical to the Excel sort function. How to do this in sql?
Hello, I need to remove duplicate spaces in a string and leave only 1, identical to the Excel sort function. How to do this in sql?
Basic Concepts
Trim: Removes all blanks regardless of the position (Left or Right) of the desired field. Ltrim: Remove the blanks that are to the left of the desired field. Rtrim: Remove the blanks that are to the right of the desired field.
Examples: Home 1 - How to remove excess white space from the following text:
' Papo Sql - Retirando espaços em Branco '
Select Trim(' Papo Sql - Retirando espaços em Branco ');
Results
'Papo Sql - Retirando espaços em Branco'
Notice that the spaces on the right and left have been eliminated, let's look at the next situation.
2 - How to remove the whitespace from the following text:
' Papo Sql - Retirando espaços em Branco à esquerda de um campo '
Select LTrim(' Papo Sql - Retirando espaços em Branco a esquerda de um campo ');
Results
'Papo Sql - Retirando espaços em Branco a esquerda de um campo '
In this case, we still have spaces to the right, however, the function fulfilled with the proposed, remove the spaces on the left, now:
3 - How to remove excess white space from the following text:
' Papo Sql - Retirando espaços em Branco à direita de um campo '
Select RTrim(' Papo Sql - Retirando espaços em Branco a direita de um campo ');
Results
' Papo Sql - Retirando espaços em Branco a direita de um campo'
This time, we have the opposite of the previous situation, removing the spaces on the right.
Returning to the initial subject, when you enter the information in the database, use the functions, thus avoiding that the text fields have unnecessary spaces.
Now, when the bank already contains whitespace, deletion should occur with the use of Update, using Trim, Ltrim or Rtrim, depending on your situation, let's look at the example:
Update tabela
Set nome = Trim(nome);
In this example, we remove all spaces to the right and left of the name column.