SQL Server Find and Replace Script
-
SQL SERVER - I'm not a SQL script guy, but I've got a SQL script problem. I'm trying to do a simply find-and-replace of a url wherever it may be in the db. The script I was given doesn't work, and the guy who wrote the script is gone. You can see the script here: http://number9media.com/scripts/FindAndReplace-qlinks.sql When I run it, I get no errors, but it doesn't make any changes. Someone pointed to the PATindex in line 22 as a potential problem, if that helps.
-
Answer:
It looks like you're taking an awful lot of trouble to do what you could do with one statement: UPDATE library SET filename = REPLACE(filename, 'original string', 'replacement string') WHERE libtype_id = 4
jpoulos at Ask.Metafilter.Com Visit the source
Other answers
But the string may be embedded within the data field. If my original string is 'John' and my replacement is 'Bob', what happens when script encounters 'The boy's name is John'? Assuming it recognizes the substring 'John', won't it replace the whole thing with 'Bob'?
jpoulos
I agree that the script is completely over-written, but I think the main problem is that the @otxt and @ntxt parameters are blank, which means that you're effectively replacing nothing with nothing. Have you re-set those parameters correctly?
mkultra
Nope, you'll get "The boy's name is Bob". The only caveat is that if you only want to replace the first occurrance of the string in the column, you'll need to do some more acrobatics, but you certainly don't need to open a cursor to do it. (If you want to see this in action, try it yourself: SELECT REPLACE('His name is John', 'John', 'Bob'))
uncleozzy
Oops. The version I posted was the original that they sent me. The one I ran DOES have values for @otxt and @ntxt. I've updated the one online. http://number9media.com/scripts/FindAndReplace-qlinks.sql
jpoulos
uncleozzy's code worked. Thank you! You just put an end to a week-long nightmare.
jpoulos
Related Q & A:
- How to control the excessive use of ram by SQL Server?Best solution by Database Administrators
- Can I create a second filestream container on an existing SQL Server 2008 database without going offline?Best solution by Database Administrators
- How to connect Sql Server Database from android app?Best solution by Stack Overflow
- How to upgrade sql server express 2005 to 2008R2?Best solution by Server Fault
- How to replace Cursor in SQL Server?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.