Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

Community Wiki

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Use REPLACE To eliminate unwanted characters

From Wiki

Jump to: navigation, search

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.

  1. CREATE TABLE #TestAddress (Address VARCHAR(100))
  2. INSERT #TestAddress VALUES( 'New address....')
  3. INSERT #TestAddress VALUES( 'New address.')
  4. INSERT #TestAddress VALUES( 'New address   .')
  5. INSERT #TestAddress VALUES( 'New .address')

Now run this query

  1. SELECT Address,REPLACE(Address,'.','')  AS AfterReplace
  2. 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.


  1. UPDATE #TestAddress
  2. 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

288 Rating: 2.6/5 (45 votes cast)