SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to clean data with the REPLACE function?

Execute the following Microsoft SQL Server T-SQL script in SSMS Query Editor to create a table of names with control characters (white space) imbedded and clean them with REPLACE:

USE TempDB;

SELECT Name = FirstName+CHAR(17)+' '+LastName+CHAR(9)
INTO Name
FROM AdventureWorks.Person.Contact;

SELECT * FROM Name;

 

UPDATE Name SET Name = REPLACE(REPLACE
(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE
(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE
(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
Name,
CHAR(1),''),CHAR(2),''),CHAR(3),''),CHAR(4),''),CHAR(5),''),CHAR(6),''),
CHAR(7),''),CHAR(8),''),CHAR(9),''),CHAR(10),''),CHAR(11),''),CHAR(12),
''),CHAR(13),''),CHAR(14),''),CHAR(15),''),CHAR(16),''),CHAR(17),''),
CHAR(18),''),CHAR(19),''),CHAR(20),''),CHAR(21),''),CHAR(22),''),CHAR(23),
''),CHAR(24),''),CHAR(25),''),CHAR(26),''),CHAR(27),''),CHAR(28),''),
CHAR(29),''),CHAR(30),''),CHAR(31),'');

SELECT * FROM Name;

Related article:

Using the SQL Server Integration Services for Data Cleansing - Fuzzy Lookups and Groupings Provide Powerful Data Cleansing Capabilities


Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.