Use REPLACE To eliminate unwanted characters
From Wiki
Data is being added through an application and in this application you don't allow dots to be entered. Now you are getting this big text file to import and you notice that there are dots in there. What can you do to eliminate these dots? Let's take a look. First create this table and insert some values.
- CREATE TABLE #TestAddress (Address VARCHAR(100))
- INSERT #TestAddress VALUES( 'New address....')
- INSERT #TestAddress VALUES( 'New address.')
- INSERT #TestAddress VALUES( 'New address .')
- INSERT #TestAddress VALUES( 'New .address')
Now run this query
- SELECT Address,REPLACE(Address,'.','') AS AfterReplace
- FROM #TestAddress
Output
| New address.... | New address |
| New address. | New address |
| New address . | New address |
| New .address | New address |
As you can see using REPLACE(Address,'.',) eliminated the dots, this is accomplished by replacing the dots with blanks. After you are satisfied that that looks right you can then run the update statement to fix the data.
- UPDATE #TestAddress
- SET Address=REPLACE(Address,'.','')
You could have also created an instead of trigger to accomplish the same thing.
Contributed by: --SQLDenis 03:04, 31 May 2008 (GMT)
Part of SQL Server Programming Hacks
Section Handy tricks


