Solved

How to recursively replace text within a column for a

Posted on 2013-02-07
2
170 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

932 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

8 Experts available now in Live!

Get 1:1 Help Now