Home > Ask the Oracle Experts > SQL Questions & Answers > The SQL REPLACE function
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

The SQL REPLACE function

Rudy Limeback EXPERT RESPONSE FROM: Rudy Limeback

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 16 July 2008
I am trying to use a REPLACE statement to replace elements of an AddressLine1 field in an attempt to remove commas, periods, hyphens, and replace '3 South ' with '3 S ' (as in 123 South Main Street to 123 S Main Street), plus 95 other things so far.

What I need to figure out is how to do a replacement of ' Road' at the end of the address to ' Rd' without changing an address such as '2884 N Roadrunner Pkwy' to '2884 N Rdrunner Pkwy'. To do this, I need to do something like REPLACE(' Road[ENDOFLINE]',' Rd[ENDOFLINE]'). But that obviously doesn't work.


>
EXPERT RESPONSE
First of all, my condolences for the onerous task of laundering addresses. This is truly one of those horrible tasks which is better performed outside of the database than with SQL (as I'm sure you already know, faced with 95 things to change).

Let's assume you're using a database system which has a REPLACE function (not all database systems do). The REPLACE you're looking for goes like this:

UPDATE yourtable
   SET AddressLine1 
     = REPLACE(AddressLine1, ' Road', ' Rd')
 WHERE AddressLine1 LIKE '% Road'

This will perform the update on only those rows that end with ' Road'. Note that there is a leading wildcard for the LIKE string, but no trailing wildcard.

Of course, this will also change '123 Roadley Road' to '123 Rdley Rd'. This is because the REPLACE itself does not look only at the end of the column value, but all through it. If you are worried about these situations, then you could enhance your WHERE clause like this:

 WHERE AddressLine1 LIKE '% Road'
   AND DATALENGTH(Replace(AddressLine1, ' Road', ' Rd')) 
     = DATALENGTH(AddressLine1)-2

This additional qualification uses the same REPLACE function, and compares the length of the result to the length of the original column value. Since we want only one occurrence to be replaced, which has to be at the end because of the LIKE condition, the new length can only be 2 less than the original (because the letters 'oa' have been removed).

Neat, eh?


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
SQL
Using the SQL date function to find aggregate totals by month
Using an SQL SELECT statement from a non-existing table
Using LEFT OUTER JOIN query to get zero row counts in SQL
How to return multiple values for THEN clause in an SQL CASE expression
Can I concatenate row values in SQL?
Should I try to avoid a LEFT OUTER JOIN in SQL?
Tips for derived tables in SQL and using FULL OUTER JOINs
How to write an SQL query for two foreign keys to the same table
How to create an SQL CHECK constraint for two letters
How to return a zero in SQL instead of no row back for a select count

Oracle development languages
Using LEFT OUTER JOIN query to get zero row counts in SQL
How to return multiple values for THEN clause in an SQL CASE expression
Can I concatenate row values in SQL?
Should I try to avoid a LEFT OUTER JOIN in SQL?
Tips for derived tables in SQL and using FULL OUTER JOINs
How to write an SQL query for two foreign keys to the same table
How to create an SQL CHECK constraint for two letters
How to return a zero in SQL instead of no row back for a select count
How to write an SQL query using GROUP BY for row analysis
Using nested SQL string functions to find ERP customer values in a table

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts