About the author

Steven HarmanSteven Harman is a passionate developer who believes that writing great software isn't just a job, its a craft.

ASP.NET MVP

For recent posts and more about me, scroll to the bottom.

Subscribe

  • Subscribe to my feed. via RSS
  • Subscribe via email via email

Jobs

Badges

  • Subtext Project
  • Support Subtext
  • HiddenNetwork.com Banner

Search & Replace - For Your Database

Search and Replace UPDATE: It appears that the .zip file I posted earlier was actually busted - I guess that's what I get for being lazy and not testing the parameterized version before putting it out there. Anyhow, Phil emailed me a version that actually works as advertised. I've updated the link at the bottom of this post, so grab the updated version from there.

Search and replace is a common (and most useful) feature that is present in even the most basic of text editors. Some of the more powerful text editors (I like notepad++) even allow you to use Regular Expressions for searching - and that really is powerful.

Wouldn't it be nice if there was a tool that would allow you to search and replace text within the fields of you database? I've wanted a tool like that forever... and I'm sure there are some great tools out there that do just that, but I've not yet found them. However, I did stumble across a very handy SQL script that will allow you to do a search and replace on a database text field... sweet!

I don't remember exactly where I found the original script, only that I originally came across it about six months ago... and then promptly forgot about it until just recently. So if you look at the code, recognize it, and happen to know where it originally came from, please leave a comment and let me know. :)

Oh, one thing I should mention is that the original script was busted, so what I've posted here is my own modified version - and it's worked for me several times.

I actually used the below script to update hyperlinks in a local Subtext database that I used for development work. Since I had pulled in some real production data to test with, I wanted to update some of they hyperlinks in the post content so that they would link back to my dev box. Take a look:

/*
* Search & Replace 
* Use Ctrl+Shift+M to replace template values
*/

set xact_abort on
begin tran

declare @otxt varchar(1000)
set @otxt = 'stevenharman.net/images/stevenharman_net/'

declare @ntxt varchar(1000)
set @ntxt = 'localhost:2732/images/localhost_2732/'

declare @txtlen int
set @txtlen = len(@otxt)

declare @ptr binary(16)
declare @pos int
declare @id int

declare curs cursor local fast_forward
for
select 
    id,
    textptr([Text]),
    charindex(@otxt, [Text])-1
from 
    subtext_Content 
where 
    [Text] 
like 
    '%' + @otxt +'%'

open curs

fetch next from curs into @id, @ptr, @pos

while @@fetch_status = 0
BEGIN
    IF @pos > -1
    BEGIN
        print 'Text found in row id=' + cast(@id as varchar) 
            + ' at pos=' + cast(@pos as varchar)
    
        updatetext subtext_Content.[Text] @ptr @pos @txtlen @ntxt
    END

    fetch next from curs into @id, @ptr, @pos    
end

close curs
deallocate curs

commit tran

Now, one thing you'll notice when running the script is that it will report back how many matches it found and replaced each time it's run. So that means you'll just need to hit F5 repeatedly until the script stops reporting matches. I realize that with just a few small tweaks I could make the script until no matches are found, but I'm being really lazy... so I'll leave that for Jon "Mr. Database" Galloway.

Oh yeah, and for your convenience I have a parameterized version that you can download below. Just drop it into Query Analyzer and hit Ctrl+Shift+M. Then fill in your database table, column name, search string, replace string, and run.

Get the script here: [Download]

Technorati tags: , ,

What others are saying.

# Re: Search & Replace - For Your Database
That rocks!
# re: Search & Replace - For Your Database
Gravatar Shiva
Feb 06, 2007
I needed a script like this to move my blog images from one host to another.

Glad I waited for till your "beta-testers" tried it first :)
# re: Search & Replace - For Your Database
Gravatar Steve Harman
Feb 06, 2007
@Shiva: If you look at the example code that I posted you'll see that I was doing the very same thing. :) I was changing from my prod box to my local dev box for testing... so it should be pretty close to what you're going to do.

Good Luck!
# re: Search & Replace - For Your Database
Gravatar Toni
Jun 22, 2007
Hi, That's a nice script.
I have read it but is there a more generic one that will replace all the fields for all the user tables in a specific database?
# re: Search & Replace - For Your Database
Gravatar Steve Harman
Jun 22, 2007
@Toni: I don't know of such a script off the top of my head, but you might be able to modify the script I laid out here to accomplish your goal.

If you're able to figure out such a query, please let me know via a comment... I'm sure others would find it useful as well.

Thanks!
# re: Search & Replace - For Your Database
Gravatar Saxar
Jun 22, 2007
I have tried both versions and I keep getting an Invalid column name 'id'. error when I run this. Should the id be replaced with something? I have tried using the tables Clustered Index and then I get - Only text, ntext, and image columns are valid with the TEXTPTR function.

The fields being changed are varchar.

It seems like a great script, but there must be something I am missing. Any help would be greatly appreciated.
# re: Search & Replace - For Your Database
Gravatar Steve Harman
Jun 22, 2007
@Saxar: Keep in mind that the Id column I'm selecting the the above script is specific to my database and table - namely, to Subtext's subtext_Content table.

Also, I think that this script only works on columns that are of type text and ntext... not for varchar, sorry.
# re: Search & Replace - For Your Database
Gravatar Saxar
Jun 22, 2007
Thanks Steve, I had tried changing the id to, my ID column I did, the varchar issue makes sense. i think I found another way to solve my problem though.

Thanks again for the quick response.
# re: Search & Replace - For Your Database
Gravatar nick
Aug 10, 2007
Hey Steve,

Thanks for posting this - minor issue though, it doesn't work on text fields longer than ~8000 chars or so. :( I'm pretty sure that's why you were getting errors before you updated the script, because I was running the same original script and discovered the problem. I'd get charindex return values of -1 for really long text fields because the script doesn't handle them properly. Your revision just hides those errors, but it leaves behind some unchanged data afterwards.
# A Better Search and Replace for Your Database
Gravatar StevenHarman.net
Dec 30, 2007
A Better Search and Replace for Your Database
Comments have been closed on this topic.