Solved

MS SQL - Find and Replace text within field

Posted on 2011-03-22
9
851 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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 …

777 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