Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Use REPLACE To eliminate unwanted characters
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
|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.
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