开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜