Solved

MS SQL - Find and Replace text within field

Posted on 2011-03-22
9
848 Views
Last Modified: 2012-05-11
Hi,

I have written this simply query

SELECT *
FROM table
WHERE field_name like '%123%'

Open in new window


I need to extend this query to replace the text "123" with "1/2/3".

This text will be part of a bigger text string in the column 'field_name' with type as 'text'.

Whats the best way to do this?

Thanks for any input.
0
Comment
Question by:SM17CH
9 Comments
 
LVL 32

Expert Comment

by:ewangoya
Comment Utility
try
UPDATE table
SET field_name = REPLACE(field_name, '123', '1/2/3')
WHERE field_name like '%123%'

Open in new window

0
 

Expert Comment

by:adamianf
Comment Utility
Hi SM17CH,
How about this:

UPDATE table
SET field_name = REPLACE(field_name,'123','1/2/3')
WHERE field_name like '%123%'

0
 
LVL 2

Author Comment

by:SM17CH
Comment Utility
Can I have multiple SET conditions eg:

UPDATE table
SET field_name = REPLACE(field_name,'123','1/2/3')
SET field_name = REPLACE(field_name,'XYZ','ABC')
WHERE.....
0
 
LVL 2

Author Comment

by:SM17CH
Comment Utility
Looks like the REPLACE() command doesnt work on a TEXT field

"Argument data type text is invalid for argument 1 of replace function."
0
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

 

Expert Comment

by:adamianf
Comment Utility
You could try using a CASE statment.
EG:

UPDATE table
SET field_name =
CASE WHEN field_name LIKE '%123%' THEN REPLACE(field_name,'123','1/2/3')
WHEN field_name like 'XYZ' THEN REPLACE (field_name,'XYZ','ABC)
ELSE field_name
END

Take Note: Don't forget the 'ELSE field_name' clause at the end. Otherwise, anything that doesn't match the criteria you are searching for will be changed to NULL.


You should create a copy of your table before you run this, just to be safe!

Good luck - if you need this explainied more clearly, drop me a line.
adam
0
 

Expert Comment

by:adamianf
Comment Utility
Oh really? I don't use Text field type, so I didn't know that.
What about converting the type to varchar, using the CONVERT function?
SOmething like
field_name = convert(text, (REPLACE (convert(varchar(255),field_name),'XYZ','ABC))
Not sure if that will work, but worth a try.
0
 
LVL 2

Accepted Solution

by:
SM17CH earned 0 total points
Comment Utility
I just succesffuly used this to solve my issue

UPDATE <Table> set textcolumn= REPLACE(SUBSTRING(textcolumn,1,DATALENGTH(textcolumn)),'findtext','replacetext')  WHERE <Condition>

HOWEVER
it apparently wont work on fields greater the 8000 characters as it will truncate the data.

Solution was found here - http://stackoverflow.com/questions/289688/t-sql-replace-on-text-field
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>it apparently wont work on fields greater the 8000 characters as it will truncate the data<<
Than that is not a very good solution, would you not agree.  Would you not prefer one that did not have that limitation?

P.S.  You do know that the text data type is deprecated, right?
0
 
LVL 2

Author Closing Comment

by:SM17CH
Comment Utility
Correct answer found on another forum.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

11 Experts available now in Live!

Get 1:1 Help Now