Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim spaces from values in MySQL table

I want to update all the values in a table by trimming all the leading and trailing spaces. I have tried the following queries but neither worked.

I know that it is possible to use TRIM with SELECT, but how can I use it with UPDATE?

UPDATES teams SET name = TRIM(name)
UPDATES teams SET name = TRIM(LEADING ' ' TRAILING ' ' FROM name)
like image 385
Dimme Avatar asked Jun 04 '12 18:06

Dimme


People also ask

How can remove space from data in MySQL?

The TRIM() function removes leading and trailing spaces from a string.

How do I remove spaces between words in MySQL?

If you have spaces between letters then you can use REPLACE() function to remove spaces.

How do you TRIM extra spaces in SQL?

SQL Server TRIM() FunctionThe TRIM() function removes the space character OR other specified characters from the start or end of a string. By default, the TRIM() function removes leading and trailing spaces from a string. Note: Also look at the LTRIM() and RTRIM() functions.

How do I TRIM a character in MySQL?

Use the TRIM() function with the LEADING keyword to remove characters at the beginning of a string. TRIM() allows you to remove specific character(s) or space(s) from the beginning, end, or both ends of a string. This function takes the following arguments: An optional keyword that specifies the end(s) to trim.


2 Answers

You do not have to SELECT.

Try this -

UPDATE teams SET name = TRIM(name)
WHERE 1 = 1;
like image 159
JHS Avatar answered Oct 06 '22 14:10

JHS


UPDATE teams SET name = TRIM(name)

That should work, it is semantically correct for MySQL.

like image 29
hannebaumsaway Avatar answered Oct 06 '22 14:10

hannebaumsaway