SQL copy one column then paste it in a new column with all white space removed
I want to copy one column in my database and paste it into another 开发者_StackOverflow中文版column with all white space removed. I do not want to use export/import but a SQL query if this is possible.
Try this
UPDATE MyTable
SET City2 = REPLACE(City, ' ', '');
This will remove all whitespace. Leading spaces, trailing spaces, all spaces.
This will turn:
- "Rio De Janerio" into "RioDeJanerio"
- "Sandusky " into "Sandusky"
- " Orangeville" into "Orangeville"
If the whitespace is leading and trailing then use the TRIM function. If it all whitespaces beetween words, trailing and leading use the replace function that @p.campbell suggested
Add the new column city2
using ALTER TABLE. And then run the following query:
update `table` set `city2` = replace(`city`, ' ', '');
However, the above is assuming that both columns are in the same table. If they are from different tables you may use JOINS in the update query. Let me know if that helps.
精彩评论