?
Solved

MS SQL - Find and Replace text within field

Posted on 2011-03-22
9
Medium Priority
?
859 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
[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
9 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35195488
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
ID: 35195495
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
ID: 35195575
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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 2

Author Comment

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

"Argument data type text is invalid for argument 1 of replace function."
0
 

Expert Comment

by:adamianf
ID: 35195600
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
ID: 35195680
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
ID: 35195684
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
ID: 35200870
>>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
ID: 35225521
Correct answer found on another forum.
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

770 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