Trim spaces from values in MySQL table - mysql

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)

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 field contain new line character then
UPDATE Tablename SET colname= TRIM(TRAILING '\r' FROM colname)

This one solves a weird problem I was having when the TRIM option does not work:
UPDATE
teams
SET
name = TRIM(BOTH UNHEX('C2A0') FROM name)

Related

Trying to find all field starting with space

My DB is acting a little weird and I figured out that coin_id of some products has a "space" as their first letter. So instead of "123456" it is " 123456". I am trying to find all the coin_id that start with a "space" and then remove the spaces.
No need to find them yourself, just trim them in an UPDATE query. Use LTRIM or just TRIM if there shouldn't be leading or trailing spaces:
UPDATE table_name SET coin_id = LTRIM(coin_id)
As Dudu Markovitz points out, especially if there are few to update of many, you can find them and update them for possibly better performance:
UPDATE table_name SET coin_id = LTRIM(coin_id) WHERE coin_id LIKE ' %'
Or for TRIM:
UPDATE table_name SET coin_id = TRIM(coin_id)
WHERE coin_id LIKE ' %' OR coin_id LIKE '% '
Update has performance Implications.
Update only what you need to update.
update t
set coin_id = ltrim(coin_id)
where coin_id like ' %'
How about this?
Removing one character from the columns starting with a space
UPDATE table
SET coin_id = RIGHT(coin_id,LENGTH(coin_id)-1)
WHERE LEFT(coin_id,1) = ' ';
First, why is an id that consists of digits stored as a string? Perhaps the "12345" is not really a representative value.
I would suggest that you use like:
update t
set coin_id = trim(coin_id)
where coin_id like ' %';
Notes:
like ' %' allows the optimizer to use an index on t(coin_id).
The query uses trim(). After spaces, at the end could also be a problem.
If the value is digits, then it should be stored as a number of some sort.

remove whitespace in between Column values in mySQL

How to remove all spaces between a column field?. The spaces occur in the middle of the text so trim won't work and also replace is not working.
my code is
UPDATE temp_emp t1, master_employee t2
SET t1.lm= t2.emp_id
where REPLACE(t1.lm, ' ', '') = REPLACE(CONCAT(t2.first_name,'',t2.last_name), ' ', '');
for example when i run the query ,
select REPLACE(lm, ' ', '') AS concat from temp_emp1
i get the output as follows
concat
----------------------------------------
rick joe
james cole
albert Th
i want the output to be ;like this
concat
----------------------------------------
rickjoe
jamescole
albertTh
Without knowing the table structures and data, it is difficult for me to follow what you are doing. However, to accomplish the ouput of two concatenated columns is very straightforward.
Assume you have a table master_employee with just two columns and you want to output the FIRST and LAST names concatenated with no spaces in between. You simply use the function concat()for MySQL:
SELECT CONCAT(first_name, last_name)
from master_employee;
In Oracle, the concatenation is two pipes (||):
SELECT first_name || last_name
from master_employee;
Hope this helps.
If you want to update the existing column which has multiple spaces into one then this update query will be helpful:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', ' ');
If you don't want any spaces then this should work:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', '');

How can I add comma to existing data

I have data with 6 characters like 123456 in a mysql database. I want to change the existing data like 123,456 . Is there any mysql query can do this. Because i want to change over 3000 records.
Try this, it will FORMAT your numbers regardless of the number of digits:
UPDATE yourtable
SET yourfield = FORMAT(yourfield,0)
Try this :
UPDATE yourtable
SET yourfield = LEFT(yourfield,3) + ',' + RIGHT(yourfield,3)
it will do what you asked for. Note it won't help if your field is not 6 characters long
update mytable set
mycolumn = replace(mycolumn, ',', '')

How do i replace "+"(plus sign) with SPACE in my database?

My database contains many + signs and i want to replace it with white-space....
What should be the query for that ?
update tbl set column = replace(column, '+', ' ')
This updates all '+'s in the column named "column" in the table named "tbl" to a single white space, one for each "+"
If you only need to select for display, then same function, e.g.
select replace(column, '+', ' ') column, other1, col3
from tbl
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘+’,‘ ’);
Documentation
For Oracle: REPLACE

MySQL replace all whitespaces with -

how could i remove ALL whitespaces from a row?
I see here alot of same question but all answers ar to use replace option. Replace will work only to strip one spaces, not all.
ex: a b c to become a-b-c
Thanks.
This can be achieved with the following MySQL Function:
SELECT REPLACE( table.field, ' ', '-' ) FROM table;
This should replace all the whitespace to a -
update image set path = REPLACE( image.path, ' ', '-' ) where path like '% %'
if you would like to update the path in mysql itself use the update for all rows which have spaces withe %20
Try this
replace('a b c',' ','-')
UPDATE table SET table.field = REPLACE( table.field, ' ', '-' );
This will update all the fields, replacing all spaces with hyphens. This will actually modify the data in the tables. Fokko's answer above will change only the data that is pulled, therefore not changing the actual data.