
- 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
Update all rows in MySQL and remove all the unnecessary whitespaces in and around the string?
To remove unnecessary whitespaces, use TRIM() in MySQL. Let us first create a −
mysql> create table DemoTable1405 -> ( -> FirstName varchar(20), -> LastName varchar(20) -> ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert −
mysql> insert into DemoTable1405 values(' Chris',' Brown '); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1405 values('David ',' Miller '); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1405 values(' Carol ',' Taylor '); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select −
mysql> select * from DemoTable1405;
This will produce the following output −
+------------------+----------------+ | FirstName | LastName | +------------------+----------------+ | Chris | Brown | | David | Miller | | Carol | Taylor | +------------------+----------------+ 3 rows in set (0.00 sec)
Following is the query to remove all the unnecessary whitespaces and update all rows −
mysql> update DemoTable1405 set FirstName=trim(FirstName),LastName=trim(LastName); Query OK, 3 rows affected (0.18 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check the table records once again −
mysql> select * from DemoTable1405;
This will produce the following output −
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Chris | Brown | | David | Miller | | Carol | Taylor | +-----------+----------+ 3 rows in set (0.00 sec)
- Related Articles
- How can we use MySQL TRIM() to remove the whitespaces from all the rows and update table?
- Remove all whitespaces from string - JavaScript
- Java Program to Remove All Whitespaces from a String
- Golang program to remove all whitespaces from a string
- Delete all whitespaces from a String in Java
- Remove all except the first character of a string in MySQL?
- Update all rows by prefixing a line to every text in MySQL?
- Update all varchar column rows to display values before slash in MySQL?
- When running UPDATE … datetime = NOW(); will all rows updated have the same date/ time in mysql?
- Remove the whitespaces from a string using replace() in JavaScript?
- MySQL query to update all records to capitalize only the first letter and set all others in lowercase
- Get all rows apart from first and last in MySQL
- MySQL: delete all rows containing string “foo” in sample table “bar”?
- MySQL Query to remove all characters after last comma in string?
- C# Program to remove whitespaces in a string

Advertisements