English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
We can usetrim()
The function removes leading and trailing spaces from MySQL.
The following is the syntax.
mysql> SELECT TRIM(' AnyStringWithWhitespaces ');
Now let's implement the above syntax in the following query.
mysql> SELECT TRIM(' Leading And Trailing whitespaces Demo ');
This is the output after removing spaces.
+---------------------------------------+ | TRIM(' Leading And Trailing whitespaces Demo ') | +---------------------------------------+ | Leading And Trailing whitespaces Demo | +---------------------------------------+ 1 row in set (0.00 sec)
Now let's see another method to remove leading and trailing spaces.
First, let's create a new table.
mysql> create table TrimDemo2 -> ( -> name varchar(200) -> );
Insert records with spaces.
mysql> insert into TrimDemo2 values(' John ');
Display records.
mysql> select *from TrimDemo2;
This is the output.
+--------------------+ | name | +--------------------+ | John | +--------------------+ 1 row in set (0.00 sec)
Now let's look at the query to remove leading and trailing spaces.
mysql> UPDATE TrimDemo2 set name = TRIM(name); Rows matched: 1 Changed: 1 Warnings: 0
This is the implementation.
mysql> select *from TrimDemo2;
The following is the output showing the leading and trailing spaces have been removed.
+------+ | name | +------+ | John | +------+ 1 row in set (0.00 sec)