Solved

How to construct SQL Search and Replace query

Posted on 2009-05-08
4
465 Views
Last Modified: 2012-05-06
I need a sql query that will search and replace the following string of text inside a field in my database table.  I want to replace the string ' with the single quote character.  Right now, I have a bunch of LastName fields that look like this:

D'Antonio
O'Block
O'Brien
O'Donnell
O'Flaherty

Again, I just want to get rid of that HTML equivalent and use the literal single quote character instead.

TIA for assistance in constructing this query!
0
Comment
Question by:dstjohnjr
  • 2
4 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
Comment Utility
UPDATE your_table_name
SET your_field_name = REPLACE(your_field_name, ''', '''')
WHERE your_field_name LIKE '%'%'
0
 
LVL 13

Expert Comment

by:usachrisk1983
Comment Utility
If REPLACE is still a valid function in MSSQL 2005 then this should work.  Backup your table first or try in test!

update tblName

   set lastName = Replace(lastName, "&#39", "'")

 where lastName like '%#39#%';

Open in new window

0
 

Author Closing Comment

by:dstjohnjr
Comment Utility
Had to give full points to this solution since A) it came in first, B) was the full working solution and C) the other solution did not work due to double quotes being used and not single quotes in the replace statement.  Thanks for expert assistance!
0
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
You are most welcome.

Glad that helped.

Regards,
Kevin
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

772 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

10 Experts available now in Live!

Get 1:1 Help Now