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)
The TRIM() function removes leading and trailing spaces from a string.
If you have spaces between letters then you can use REPLACE() function to remove spaces.
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.
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.
You do not have to SELECT
.
Try this -
UPDATE teams SET name = TRIM(name)
WHERE 1 = 1;
UPDATE teams SET name = TRIM(name)
That should work, it is semantically correct for MySQL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With