MySQL batch removes spaces in a certain field

MySQL batch removes spaces in a certain field

Is there any way to remove spaces from a certain field in batches in Mysql? Not only the spaces before and after the string, but also the spaces in the middle of the string. The answer is replace, using MySQL's own replace function. There is also a trim function.

(1) MySQL replace function

Syntax: replace(object,search,replace)

Meaning: Replace all occurrences of search in object with replace

Example: Clear spaces in the content field in the news table update `news` set `content`=replace(`content`,' ','');

(2) MySQL trim function

Syntax: trim([{BOTH | LEADING | TRAILING} [remstr] FROM] str)

The following examples illustrate:

mysql> SELECT TRIM(' phpernote '); 
-> 'phpernote' 
mysql> SELECT TRIM(LEADING 'x' FROM 'xxxphpernotexxx'); 
-> 'phpernotexxx' 
mysql> SELECT TRIM(BOTH 'x' FROM 'xxxphpernotexxx'); 
-> 'phpernote' 
mysql> SELECT TRIM(TRAILING 'xyz' FROM 'phpernotexxyz'); 
-> 'phpernotex' 

When inserting data into the MySQL database, due to negligence, there was an extra space in front of some lines of a column of text. Out of obsessive-compulsive disorder and to avoid possible problems in the future, I decided to remove this space. Most of the methods I found on the Internet are to use replace directly:

UPDATE example
SET col = replace(col, ' ', '')
WHERE col replace '^ ';

Although this method can remove the spaces at the beginning of the line, the spaces between the characters in the column used to separate words will also be removed, and a large piece of text will be connected, so it is not feasible.

Later, I wanted to see if I could use regular expressions to match when using replace. After searching for a while, I found that it was not feasible.

Finally, I thought of a method that I think is relatively simple and feasible:

First use CONCAT() to add an 'x' character to the front of the lines that have spaces:

UPDATE example
SET col = CONCAT('x', col)
WHERE col REGEXP '^ ';

This adds two characters 'x' to the front of the line.

Then use REPLACE to remove the two extra characters together:

UPDATE example
SET col = REPLACE(col, 'x ', '')
WHERE col REGEXP '^x ';

Summarize

The above is all about how to remove spaces from a certain field in MySQL in batches. I hope it will be helpful to you. Interested friends can refer to: MySQL declaration of variables and stored procedure analysis, several relatively important MySQL variables, MySQL database development specifications [recommended], etc. If you have any questions, please leave a message at any time and we can discuss together.

You may also be interested in:
  • Use replace in mysql to replace part of the content of a field
  • Detailed explanation of the usage of replace into statement in MySQL
  • MYSQL REPLACE and ON DUPLICATE KEY UPDATE statements introduce problem solving examples
  • The real difference between MySQL's Replace into and Insert into on duplicate key update
  • Detailed explanation of the use of replace syntax in MYSQL batch replacement

<<:  js drag and drop table to realize content calculation

>>:  Detailed explanation of how to pass password to ssh/scp command in bash script

Recommend

Classification of web page color properties

Classification of color properties Any color can ...

A brief discussion on how Tomcat breaks the parent delegation mechanism

Table of contents JVM Class Loader Tomcat class l...

Summary of Seven Basic XHTML Coding Rules

1. All tags must have a corresponding end tag Prev...

Incredible CSS navigation bar underline following effect

The first cutter in China github.com/chokcoco Fir...

MySQL 5.6.33 installation and configuration tutorial under Linux

This tutorial shares the installation and configu...

Linux gzip command compression file implementation principle and code examples

gzip is a command often used in Linux systems to ...

HTML table tag tutorial (20): row background color attribute BGCOLOR

The BGCOLOR attribute can be used to set the back...

Common ways to optimize Docker image size

The Docker images we usually build are usually la...

Discussion on CSS style priority and cascading order

In general : [1 important flag] > [4 special fl...

How to implement two-way binding function in vue.js with pure JS

Table of contents First, let's talk about the...

Tutorial on using Docker Compose to build Confluence

This article uses the "Attribution 4.0 Inter...