Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 512525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:19:43+00:00 2026-05-13T07:19:43+00:00

I have a SQL Server table that contains postal addresses. In preparation for mailing,

  • 0

I have a SQL Server table that contains postal addresses. In preparation for mailing, I need to do a number of string replacements to conform with USPS preferences (“Avenue” becomes “Ave”, for example).

To save me the trouble of enumerating all of the replacements, I have the label/abbreviation pairs saved in a two-column table. Is there an elegant way to pass each of those as parameters in the Replace function inside an update statement?

The lookup table looks like this:

CREATE TABLE addressLookup (label varchar(50),abbreviation varchar(20))
INSERT INTO addressLookup (label,abbreviation)
SELECT 'Avenue','Ave' UNION
SELECT 'Boulevard','Blvd' UNION
SELECT 'Drive','Dr' UNION
SELECT 'Lane','Ln' UNION
SELECT 'Street','St' UNION
SELECT 'First','1st' UNION
SELECT 'Second','2nd' UNION
SELECT 'Third','3rd' UNION
SELECT 'Fourth','4th' UNION
SELECT 'Fifth','5th' UNION
SELECT 'Sixth','6th' UNION
SELECT 'Seventh','7th' UNION
SELECT 'Eighth','8th' UNION
SELECT 'Ninth','9th' UNION
SELECT 'Tenth','10th' UNION
SELECT 'Eleventh','11th' UNION
SELECT 'Twelfth','12th' UNION
SELECT 'Apartment','Apt' UNION
SELECT 'Apartments','Apts' UNION
SELECT 'Floor','Fl' UNION
SELECT 'Room','Rm' UNION
SELECT 'Suite','Ste' UNION
SELECT 'Po Box','PO Box' UNION
SELECT 'P O Box','PO Box' UNION
SELECT 'P o Box','PO Box' UNION
SELECT 'Rural Route','RR' UNION
SELECT 'R Rte','RR' UNION
SELECT 'Rr','RR'

And this would be an example of the data being operated on (I know it’s sloppy, but this is just an example):


CREATE TABLE addresses (userid int PRIMARY KEY, address1 varchar(50), address2 varchar(50), address3 varchar(50), city varchar(50), state varchar(50), zip varchar(50))
INSERT INTO addresses (userid,address1,address2,address3,city,state,zip)
SELECT 10,'Indiana University','123 University Lane','Campus Box 123','Bloomington','IN','47405'

The problem with the update is that an arbitrary number of records from the addressLookup table could match the contents of the address table. I guess I could implement a recursive stored procedure to do the job, but I was hoping someone would have a better, more elegant solution.

Edit

Just to clarify, the Addresses table has already been populated (with several million records). I was just trying to anticipate anyone who might complain about needing real data to test a solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:19:44+00:00Added an answer on May 13, 2026 at 7:19 am

    You could probably do this using a CURSOR. but using Sql Server 2005 CTE you can try this.

    Here is a full working sample:

    DECLARE @addressLookup TABLE (label varchar(50),abbreviation varchar(20))
    INSERT INTO @addressLookup (label,abbreviation)
    SELECT 'Avenue','Ave' UNION
    SELECT 'Boulevard','Blvd' UNION
    SELECT 'Drive','Dr' UNION
    SELECT 'Lane','Ln' UNION
    SELECT 'Street','St' UNION
    SELECT 'First','1st' UNION
    SELECT 'Second','2nd' UNION
    SELECT 'Third','3rd' UNION
    SELECT 'Fourth','4th' UNION
    SELECT 'Fifth','5th' UNION
    SELECT 'Sixth','6th' UNION
    SELECT 'Seventh','7th' UNION
    SELECT 'Eighth','8th' UNION
    SELECT 'Ninth','9th' UNION
    SELECT 'Tenth','10th' UNION
    SELECT 'Eleventh','11th' UNION
    SELECT 'Twelfth','12th' UNION
    SELECT 'Apartment','Apt' UNION
    SELECT 'Apartments','Apts' UNION
    SELECT 'Floor','Fl' UNION
    SELECT 'Room','Rm' UNION
    SELECT 'Suite','Ste' UNION
    SELECT 'Po Box','PO Box' UNION
    SELECT 'P O Box','PO Box' UNION
    SELECT 'P o Box','PO Box' UNION
    SELECT 'Rural Route','RR' UNION
    SELECT 'R Rte','RR' UNION
    SELECT 'Rr','RR'
    
    
    DECLARE @addresses TABLE (userid int PRIMARY KEY, address1 varchar(50), address2 varchar(50), address3 varchar(50), city varchar(50), state varchar(50), zip varchar(50))
    INSERT INTO @addresses (userid,address1,address2,address3,city,state,zip)
    SELECT 10,'Indiana University','123 University Lane Suite','Campus Box 123','Bloomington','IN','47405'
    
    ;WITH CTE AS(
            SELECT  *,
                    ROW_NUMBER() OVER (ORDER BY label) RowID
            FROM    @addressLookup
    ),
    CTERep AS(
            SELECT  CTE.*,
                    userid,
                    REPLACE(address1, CTE.label,CTE.abbreviation) address1,
                    REPLACE(address2, CTE.label,CTE.abbreviation) address2,
                    REPLACE(address3, CTE.label,CTE.abbreviation) address3,
                    REPLACE(city, CTE.label,CTE.abbreviation) city,
                    REPLACE(state, CTE.label,CTE.abbreviation) state,
                    REPLACE(zip, CTE.label,CTE.abbreviation) zip,
                    1 AS Depth
            FROM    CTE, @addresses a
            WHERE   RowID = 1
            UNION ALL
            SELECT  CTE.*,
                    CTERep.userid,
                    REPLACE(CTERep.address1, CTE.label,CTE.abbreviation) address1,
                    REPLACE(CTERep.address2, CTE.label,CTE.abbreviation) address2,
                    REPLACE(CTERep.address3, CTE.label,CTE.abbreviation) address3,
                    REPLACE(CTERep.city, CTE.label,CTE.abbreviation) city,
                    REPLACE(CTERep.state, CTE.label,CTE.abbreviation) state,
                    REPLACE(CTERep.zip, CTE.label,CTE.abbreviation) zip,
                    CTERep.Depth + 1
            FROM    CTE INNER JOIN
                    CTERep ON CTE.RowID = CTERep.RowID + 1
    )
    SELECT  userid,
            address1,
            address2,
            address3,
            city,
            state,
            zip
    FROM    CTERep
    WHERE   Depth = (SELECT COUNT(*) FROM @addressLookup)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 245k
  • Answers 245k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The short answer is that you can't do what you're… May 13, 2026 at 8:12 am
  • Editorial Team
    Editorial Team added an answer This message is your clue: GCJ has been detected as… May 13, 2026 at 8:12 am
  • Editorial Team
    Editorial Team added an answer Couldn't you just play your "boing" sound when contact is… May 13, 2026 at 8:12 am

Related Questions

I wan't sure how to word this question so I'll try and explain. I
I have a SQL Server table that contains users & their grades. For simplicity's
I have a table in a sql server 2008 database that contains bunch of
I have a database with a table that contains Name and CompanyName . They
I have a legacy MS Access 2007 table that contains 52 fields (1 field

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.