
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Use TRIM on all records in a MySQL table?
TRIM is used to remove leading and trailing spaces. Let us first create a table −
mysql> create table DemoTable ( StudentName varchar(100) ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command. Here, we have inserted records with leading and trailing whitespaces −
mysql> insert into DemoTable values(' Adam Smith '); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values(' David Miller '); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(' Chris Brown '); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(' Carol Taylor '); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------------------------------+ | StudentName | +-----------------------------------+ | Adam Smith | David Miller | | Chris Brown | | Carol Taylor | +-----------------------------------+ 4 rows in set (0.00 sec)
Following is the query to trim all records in a MySQL table −
mysql> update DemoTable set StudentName=trim(StudentName); Query OK, 4 rows affected (0.24 sec) Rows matched: 4 Changed: 4 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output. All the leading and trailing whitespaces are now removed successfully using TRIM() &minuss;
+-----------------+ | StudentName | +-----------------+ | Adam Smith | | David Miller | | Chris Brown | | Carol Taylor | +-----------------+ 4 rows in set (0.00 sec)
- Related Articles
- Delete all records from a table in MySQL?
- Delete all the records from a MySQL table?
- How to delete all the duplicate records in a MySQL table?
- How can we use MySQL TRIM() to remove the whitespaces from all the rows and update table?
- Can we use INTERVAL keyword while inserting date records in a MySQL table?
- Add a single year to all the date records in a MySQL table column
- Select and filter the records on month basis in a MySQL table?
- Select records from a table on the basis of keywords in MySQL
- How can we fetch all the records from a particular MySQL table?
- Should I use COUNT(*) to get all the records in MySQL?
- A single MySQL query to insert records (not all) in the second table from the first table
- How to convert all the records in a MySQL table from uppercase to lowercase?
- Count duplicates records in MySQL table?
- How to display all constraints on a table in MySQL?
- Use UNION ALL to insert records in two tables with a single query in MYSQL

Advertisements