SoFunction
Updated on 2025-04-08

postgresql replace spaces, line breaks and carriage return operations

I won't say much nonsense, let's just read the code~

UPDATE table set name = trim(name);// Used to delete spaces before and after dataUPDATE table set name = rtrim(name);// Used to delete spaces before dataUPDATE table set name = ltrim(name);// Used to delete the space after dataUPDATE table set name = REPLACE(name,' ','');// Used to delete spaces in dataUPDATE table set name =REPLACE(name, CHR(10), '') //Replace newline charactersUPDATE table set name =REPLACE(name, CHR(13), '') //Replace the carriage return

Supplement: Postgresql query for newlines and carriage return characters:

1. Sometimes, business errors occur due to carriage return and line breaks.

The first step is to query the data of the carriage return character and line break character:

-- usechr(13)andchr(10)Conduct a query
SELECT * from Data table name WHERE "Field" like '%'||chr(13)||'%' or "Field" like '%'||chr(10)||'%';

-- In fact, querychr(13)andchr(10)All can query the data
SELECT * from Data table name WHERE "Field" like '%'||chr(13)||'%';
SELECT * from Data table name WHERE "Field" like '%'||chr(10)||'%';

-- chr(int) Convert numbers to characters;For example,select chr(65); A

For those who are confused about carriage return and line breaks, you can briefly understand:

Enter \r Originally meaning is that the cursor returns to the beginning of this line. The English return of r, and the control character can be written as CR, that is, Carriage Return

Line break \n Originally meaning is to go to the next line (not necessarily to the beginning of the next line). The English newline of n, the control character can be written as LF, that is, Line Feed

symbol ASCIIcode  significance

\n  10  Line breakNL

\r  13  EnterCR

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.