Solved

How to recursively replace text within a column for a

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

810 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