Solved

MS SQL - Find and Replace text within field

Posted on 2011-03-22
9
854 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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