There are 4 functions that can remove spaces: 1. The replace() function can replace the spaces in the string with null characters to remove all spaces. The syntax is "replace(string,' ',' ')"; 2. The trim() function can remove the spaces at the beginning and end of the string, the syntax is "TRIM(string)"; 3. The LTRIM() function can remove the spaces on the left side of the string, the syntax is "LTRIM(string)" )"; 4. The RTRIM() function can remove the spaces on the right side of the string, the syntax is "RTRIM(string)".
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
There are several commonly used methods to clear string spaces in mysql:
One is to use the trim function, which has two branches: LTRIM and RTRIM functions
The other is to directly replace the character replacement function to clear it
1. Use the mysql replace function to remove spaces
Syntax: replace(object,search,replace)
Meaning: Replace all occurrences of search in object with replace
Case: Clear news Spaces in the content field in the table
update `news` set `content`=replace(`content`,' ','');
Note: The replace function can also be used to remove a certain character
Remove all specified ordinary characters in the string, such as: [
update table set column1= REPLACE(column1,'[','') ;
Remove all specified special characters in the string, such as: single quote ', please note that you need to use backslash\escaping
update table set column1= REPLACE(column1,'\'','') ;
2. Use the mysql trim function to remove leading and trailing spaces
Full format: TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str)
Simplified format: TRIM([remstr FROM] str)
The following examples:
mysql> SELECT TRIM(' phpernote '); -> 'phpernote'
mysql> SELECT TRIM(LEADING 'x' FROM 'xxxphpernotexxx'); -> 'phpernotexxx'
mysql> SELECT TRIM(BOTH 'x' FROM 'xxxphpernotexxx'); -> 'phpernote'
3, Use the mysql LTRIM function to remove left spaces
Syntax: LTRIM(str)
Example:
mysql> SELECT LTRIM(' barbar'); -> 'barbar'
4 , use mysql RTRIM function to remove right spaces
Syntax: RTRIM(str)
Example:
mysql> SELECT RTRIM('barbar '); -> 'barbar'
[Related recommendations :mysql video tutorial】
The above is the detailed content of What are the functions to remove spaces in mysql?. For more information, please follow other related articles on the PHP Chinese website!