Solved

How to recursively replace text within a column for a

Posted on 2013-02-07
2
169 Views
Last Modified: 2013-02-11
I have a table [tbl_Customer] where I have a column that contains a customer's name [Customer_Name].  It contains first and last names together with some middle initials.  

I'd like to be able to run an update where I can remove the middle initial of certain letters.  

The select statement below, for example, returns a list of customers where I want to remove the middle initial E:

SELECT     Customer_Name
FROM         tbl_Customer WHERE     (Customer_Name LIKE '% E %')

Returns:

Jane E Public
John E Public

When the replace is complete, I would like the name to remove the selected initial and the table would contain the following for the two entries shown above:

Jane Public
John Public

Thanks, in advance, for your help.
0
Comment
Question by:robbid
2 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 38864783
DECLARE @save_updated_values TABLE (
    Customer_Id INT,
    Customer_Name nvarchar(100)
    )

UPDATE tbl_Customer
SET
    Customer_Name = REPLACE(Customer_Name, ' E ', '')
OUTPUT
    INSERTED.Customer_Id, INSERTED.Customer_Name INTO @save_updated_values
WHERE
    Customer_Name LIKE '% E %'

SELECT *
FROM @save_updated_values
ORDER BY
    Customer_Name
0
 

Author Comment

by:robbid
ID: 38876635
I was also able to do the same with this code:


UPDATE tbl_Customer
SET Customer_Name = REPLACE(Customer_Name,' W ',' ')
WHERE Customer_Name LIKE '% W %'

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now