Cleanup trailing spaces from multiple fields
Posted on 2012-08-25
I am in need of a solution to a problem which I have inadvertently created during my build of my MS SQL 2008 database. I have multiple tables, each with multiple fields which may contain data with trailing and possibly leading spaces. I am looking for a function/procedure/statement which will loop through every char/nvarchar field and eliminate the offending spaces.
I am aware that I can individually run Update statements using RTRIM/LTRIM for each field name. However, because I have hundreds of such fields I am hoping that someone has a more automated way to cleanup a whole table once. It would most likely process field 1 through field n and if the field is char/nvarchar then the update statement will execute for that field, otherwise it will go to the next field.