Solved

Search and Replace script for Ntext field

Posted on 2008-09-30
9
429 Views
Last Modified: 2010-04-21
I need a sql query / script that will search and replace a text string in 1 of the tables & fields of my database.  I know the DB, Table and Field name.  I just need to search and replace %SearchString% with %ReplaceString% (for example).

TIA!
0
Comment
Question by:dstjohnjr
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22609257
you mean, soething like this:
UPDATE yourtable 
  SET yourfield = REPLACE(yourfield, 'SearchString', 'ReplaceString')
WHERE yourfield LIKE '%SearchString%'

Open in new window

0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22609295
You have to cast the field as an nvarchar(max) first.
UPDATE yourtable 
  SET yourfield = REPLACE(cast(yourfield as nvarchar(max)), 'SearchString', 'ReplaceString')
WHERE yourfield LIKE '%SearchString%'

Open in new window

0
 

Author Comment

by:dstjohnjr
ID: 22609393
Ok, the first one by angellll did not work.  It renders this error:

Msg 8116, Level 16, State 1, Line 1
Argument data type ntext is invalid for argument 1 of replace function.

Then, trying Brandons.  Better luck, but still only replaced two occurences of my string... when I know others exist.  Odd.  Hmmm....  Any ideas?
0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22609432
I indeed overlooked the NTEXT part, sorry.

now, with SQL 2008, you should NOT use NTEXT anymore, but NVARCHAR(MAX), and the UPDATE will work correctly.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22609438
No because the replace is a replace all, unless your db or server is set for case sensitive sort order.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22609445
If you are using case sensitive SQL, then it won't replace 'ABC' if you are searching for 'abc'.
0
 

Author Comment

by:dstjohnjr
ID: 22609447
I figured it out!  Here was my final code.  I was using an actual double quote character when it actually resided in my field as "

UPDATE [VERIATECH_SHARED_DNN].[dbo].[VDNN_HtmlText]
  SET [DesktopHtml] = REPLACE(cast([DesktopHtml] as nvarchar(max)), 'src="/Portals/0/', 'src="/Portals/VeriaTech/')
WHERE [DesktopHtml] LIKE '%src="/Portals/0/%'

Thanks for the assistance!
0
 

Author Comment

by:dstjohnjr
ID: 22609462
and for the record, this is in the latest version of DotNetNuke - freshly installed, version 4.9.0 I believe.  Was migrating a site from one server using SQL Server 2005 to our new server which uses SQL Server 2008.  Had some pathing issues I had to fix and didn't want to go through them one by one.
0
 

Author Closing Comment

by:dstjohnjr
ID: 31501726
Thank you Brandon!  Great solution provided once I figured out the deal with the quote char (which actually resided in my db as ").
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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