
- 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
How to strip all spaces from a column in MySQL?
To strip all spaces from a column in MySQL, you can use REPLACE() function. Following is the syntax −
update yourTableName set yourColumnName=REPLACE(yourColumnName,' ', '' );
Let us first create a table −
mysql> create table stripAllSpacesDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.56 sec)
Following is the query to insert records in the table using insert command −
mysql> insert into stripAllSpacesDemo(Name) values('Jo h n'); Query OK, 1 row affected (0.19 sec) mysql> insert into stripAllSpacesDemo(Name) values(' Joh n'); Query OK, 1 row affected (0.16 sec) mysql> insert into stripAllSpacesDemo(Name) values('Jo hn'); Query OK, 1 row affected (0.14 sec) mysql> insert into stripAllSpacesDemo(Name) values('J ohn'); Query OK, 1 row affected (0.17 sec) mysql> insert into stripAllSpacesDemo(Name) values('John '); Query OK, 1 row affected (0.14 sec)
Following is the query to display all records from the table using select statement −
mysql> select * from stripAllSpacesDemo;
This will produce the following output −
+----+-----------+ | Id | Name | +----+-----------+ | 1 | Jo h n | | 2 | Joh n | | 3 | Jo hn | | 4 | J ohn | | 5 | John | +----+-----------+ 5 rows in set (0.00 sec)
Here is the query to strip all spaces from column in MySQL −
mysql> update stripAllSpacesDemo set Name=REPLACE(Name,' ',''); Query OK, 5 rows affected (0.12 sec) Rows matched: 5 Changed: 5 Warnings: 0
Let us display all records from the table once again to check all spaces have been stripped or not −
mysql> select * from stripAllSpacesDemo;
This will produce the following output −
+----+------+ | Id | Name | +----+------+ | 1 | John | | 2 | John | | 3 | John | | 4 | John | | 5 | John | +----+------+ 5 rows in set (0.00 sec)
- Related Articles
- How to strip all spaces out of a string in PHP?
- How to select a column name with spaces in MySQL?
- Strip last two characters of a column in MySQL?
- How to strip spaces/tabs/newlines using Python regular expression?
- How to subtract seconds from all the date records in a MySQL column?
- How to remove all instances of a specific character from a column in MySQL?
- How to remove double or more spaces from a string in MySQL?
- How to subtract the same amount from all values in a column with MySQL?
- Set Blank spaces in column names with MySQL?
- MySQL strip time component from datetime?
- How to remove all special characters, punctuation and spaces from a string in Python?
- Java Program to remove all white spaces from a String.
- Removing all spaces from a string using JavaScript
- How to select all the characters after the first 20 characters from a column in MySQL?
- How to concatenate all values of a single column in MySQL?

Advertisements