Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Search and Replace script for Ntext field

Posted on 2008-09-30
9
Medium Priority
?
438 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 2000 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
DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

 
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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 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