Home > Database > Mysql Tutorial > What are the functions to remove spaces in mysql?

What are the functions to remove spaces in mysql?

青灯夜游
Release: 2022-06-14 14:10:41
Original
19063 people have browsed it

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)".

What are the functions to remove spaces in mysql?

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`,' ','');
Copy after login

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,'[','') ;
Copy after login
  • 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,'\'','') ;
Copy after login

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'
Copy after login
mysql> SELECT TRIM(LEADING 'x' FROM 'xxxphpernotexxx'); 
-> 'phpernotexxx'
Copy after login
mysql> SELECT TRIM(BOTH 'x' FROM 'xxxphpernotexxx'); 
-> 'phpernote'
Copy after login

3, Use the mysql LTRIM function to remove left spaces

Syntax: LTRIM(str)

Example:

mysql> SELECT LTRIM(' barbar');
-> 'barbar'
Copy after login

4 , use mysql RTRIM function to remove right spaces

Syntax: RTRIM(str)

Example:

mysql> SELECT RTRIM('barbar ');
-> 'barbar'
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template