Global Substring Find and replace in SQL
Last Post 02/15/2007 4:40 PM by Adam Lau. 3 Replies.
Author Messages
Shane Jennings
Nuke Active Member
Nuke Active Member
Posts:32


--
02/14/2007 12:01 PM  
I recently made a change to a portal path (I am simplifying this) and I need to do a global find and replace in SQL to change the path to the portal folder.  I don't think alternative methods to changing the portal path will work because this situation is a bit complicated and I am almost certain this is the only way for me to do this.

That being said, does anyone know how to do a global find and replace for a substring in a SQL database?

I want to search for  “/oldVirtualFolderName/Portals/0/”

 and replace it with “/Portals/0/”

SELECT [Content]
  FROM [dnn].[dbo].[ContentManager]
 WHERE [Content] LIKE '%/oldVirtualFolderName/Portals/0/%'

will return a list of relevant fields but I do not know how to apply an update to the substring.

Can anyone tell me how this is possible?
Andy Hock
Nuke Pro
Nuke Pro
Posts:90


--
02/14/2007 5:39 PM  
Hey:

I'm not sure where you're going with this.  From your example, you are searching for columns [content] in a specific table named [contentmanager].  If you want to search *all* columns for *all* tables in a database for specific content, you'd need to build a query using the system tables, checking that the column is a string 'type' column (char, varchar, etc), and then apply the select--and that's just to list all of the columns in all of the tables that match.

Perhaps I'm looking too far into what you're doing.  If you wanted to update values in a specific column from a specific table in a database, then using your example it would be:

UPDATE dnn].[dbo].[ContentManager]
SET [Content = '/Portals/0/'
WHERE [Content] LIKE '%/oldVirtualFolderName/Portals/0/%'

But I don't think that's what you're talking about.  It's pretty complex to build a stored proc that will replace values throughout an entire database.  The place to ask this question is on www.experts-exchange, in the MSSQL category.  An expert there would be able to help ya.

Hope that helps.

Posted By sjennings on 14 Feb 2007 12:01 PM
I recently made a change to a portal path (I am simplifying this) and I need to do a global find and replace in SQL to change the path to the portal folder.  I don't think alternative methods to changing the portal path will work because this situation is a bit complicated and I am almost certain this is the only way for me to do this.

That being said, does anyone know how to do a global find and replace for a substring in a SQL database?

I want to search for  “/oldVirtualFolderName/Portals/0/”

 and replace it with “/Portals/0/”

SELECT [Content]
  FROM [dnn].[dbo].[ContentManager]
 WHERE [Content] LIKE '%/oldVirtualFolderName/Portals/0/%'

will return a list of relevant fields but I do not know how to apply an update to the substring.

Can anyone tell me how this is possible?



DotNetNuke and .NET web parts, web services, modules and code snippets.
Andy Hock
Nuke Pro
Nuke Pro
Posts:90


--
02/14/2007 5:40 PM  
Posted By andyhock on 14 Feb 2007 5:39 PM
Hey:

I'm not sure where you're going with this.  From your example, you are searching for columns [content] in a specific table named [contentmanager].  If you want to search *all* columns for *all* tables in a database for specific content, you'd need to build a query using the system tables, checking that the column is a string 'type' column (char, varchar, etc), and then apply the select--and that's just to list all of the columns in all of the tables that match.

Perhaps I'm looking too far into what you're doing.  If you wanted to update values in a specific column from a specific table in a database, then using your example it would be:

UPDATE dnn].[dbo].[ContentManager]
SET [Content] = '/Portals/0/'
WHERE [Content] LIKE '%/oldVirtualFolderName/Portals/0/%'

But I don't think that's what you're talking about.  It's pretty complex to build a stored proc that will replace values throughout an entire database.  The place to ask this question is on www.experts-exchange, in the MSSQL category.  An expert there would be able to help ya.

Hope that helps.

Posted By sjennings on 14 Feb 2007 12:01 PM
I recently made a change to a portal path (I am simplifying this) and I need to do a global find and replace in SQL to change the path to the portal folder.  I don't think alternative methods to changing the portal path will work because this situation is a bit complicated and I am almost certain this is the only way for me to do this.

That being said, does anyone know how to do a global find and replace for a substring in a SQL database?

I want to search for  “/oldVirtualFolderName/Portals/0/”

 and replace it with “/Portals/0/”

SELECT [Content]
  FROM [dnn].[dbo].[ContentManager]
 WHERE [Content] LIKE '%/oldVirtualFolderName/Portals/0/%'

will return a list of relevant fields but I do not know how to apply an update to the substring.

Can anyone tell me how this is possible?





DotNetNuke and .NET web parts, web services, modules and code snippets.
Adam Lau
Nuke Newbie
Nuke Newbie
Posts:6


--
02/15/2007 4:40 PM  
I use a product called DBGhost at work to help with SQL database change management. One of it's functions is to script out the whole database (schema and data) for you as text files.  You can then use your preferred text editor to search and replace your string within all the files. Once complete you can you DBGhost to build the database for you again using the scripts. It will handle sql object dependencies and apply a bunch of checks.

I believe a trial edition is available for you to test this out.

There maybe other products that do this, however, I only have experience with this one . . .


---