
- 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 remove double or more spaces from a string in MySQL?
You can create a function to remove double or more spaces from a string. The syntax is as follows:
DELIMITER // create function yourFunctionName(paramter1,...N) returns datatype; begin //your statement. end; // DELIMITER ;
Here’s how to create a function:
mysql> delimiter // mysql> create function function_DeleteSpaces(value varchar(200)) returns varchar(200) -> begin -> set value = trim(value); -> while instr(value, ' ') > 0 do -> set value = replace(value, ' ', ' '); -> end while; -> return value; -> END; -> // Query OK, 0 rows affected (0.20 sec) mysql> delimiter ;
Now you can call the function using a select statement. The syntax is as follows:
SELECT yourFunctionName();
Call the above function using a select statement. The above function removes the spaces from the string:
mysql> select function_DeleteSpaces(' John Smith ');
The following is the output:
+--------------------------------------------------+ | function_DeleteSpaces(' John Smith ') | +--------------------------------------------------+ | John Smith | +--------------------------------------------------+ 1 row in set (0.02 sec)
The above function removes more than one spaces. Let us see another example with a new value in the function’s parameter:
mysql> select function_DeleteSpaces(' John Smith 123 ');
The following is the output:
+---------------------------------------------------------+ | function_DeleteSpaces(' John Smith 123 ') | +---------------------------------------------------------+ | John Smith 123 | +---------------------------------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- C++ Program to remove spaces from a string?
- Remove spaces from std::string in C++
- C++ program to remove spaces from a string using String stream
- Java Program to remove all white spaces from a String.
- How to transform two or more spaces in a string in only one space? JavaScript
- How to remove all special characters, punctuation and spaces from a string in Python?
- How to remove white spaces (leading and trailing) from string value in MongoDB?
- Java program to remove all the white spaces from a given string
- MySQL update query to remove spaces?
- MySQL query to remove trailing spaces
- Remove extra spaces in string JavaScript?
- How can we remove all the prefixes or suffixes from a given string in MySQL?
- Trim a string in Java to remove leading and trailing spaces
- C program to remove spaces in a sentence using string concepts.
- How to remove spaces at the end in string vectors in R?

Advertisements