TRIM用于删除前导和尾随空格。让我们首先创建一个表-
mysql> create table DemoTable ( StudentName varchar(100) );
使用insert命令在表中插入一些记录。在这里,我们插入了带有前导和尾随空格的记录-
mysql> insert into DemoTable values(' Adam Smith '); mysql> insert into DemoTable values(' David Miller '); mysql> insert into DemoTable values(' Chris Brown '); mysql> insert into DemoTable values(' Carol Taylor ');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------------------------------+ | StudentName | +-----------------------------------+ | Adam Smith | David Miller | | Chris Brown | | Carol Taylor | +-----------------------------------+ 4 rows in set (0.00 sec)
以下是修剪MySQL表中所有记录的查询-
mysql> update DemoTable set StudentName=trim(StudentName); Rows matched: 4 Changed: 4 Warnings: 0
让我们再次检查表记录-
mysql> select *from DemoTable;
这将产生以下输出。现在,可以使用TRIM()
&minuss;成功删除所有开头和结尾的空格。
+-----------------+ | StudentName | +-----------------+ | Adam Smith | | David Miller | | Chris Brown | | Carol Taylor | +-----------------+ 4 rows in set (0.00 sec)