Solved

How to recursively replace text within a column for a

Posted on 2013-02-07
2
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

710 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