Sunday, January 2, 2011

Clean up the white space.

Don't know how it happens, but sometimes we get blank, or empty characters within our data values.  I call this the 'white space'.  You can't really see it under normal conditions, but it definitely will hinder the accuracy of your data collections.  There's a very quick way to find AND fix it.

     --Find them
     SELECT '('+FieldName+')'
     FROM dbo.TableName
     WHERE some condition exists
  
     --Fix them (remove the whitespace)
     UPDATE dbo.TableName
     SET FieldName = LTRIM(RTRIM(FieldName))
     WHERE some condition exists 


If you've got a LOT of values with white space, you're definitely going to want to be careful with that update.  Let me know if that's the case.  Also, please understand that the above will only handle leading and trailing blank spaces.  (LTRIM = leading, RTRIM = trailing)  If you've got blank, or empty characters within the string values, you'll need to do something like this:


     UPDATE dbo.TableName

     SET FieldName = REPLACE(FieldName,' ','')

That will REPLACE any space in the targeted field with no space.  Always smart to look at the before and after side by side, before performing the update:

     SELECT FieldName, REPLACE(FieldName,' ','')

     FROM dbo.TableName



No comments:

Post a Comment