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
Solved

MS SQL - Find and Replace text within field

Posted on 2011-03-22
9
852 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
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Text file into sql server 5 32
Currency in SQL? 2 30
MS SQL Server time between records 14 46
SQL Query Syntax to add subtotal calculation by Contractor for each row. 3 25
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…
In this article I will describe the Detach & Attach 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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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