Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 File Paths stored in SQL

Author  Topic 

boblotz2001
Starting Member

1 Post

Posted - 2013-01-24 : 09:53:18
Have an annoying problem in a table that stores metadata to files stored elsewhere. The file paths are stored every which way including local paths and UNCs. To add insult to injury the application seems to use back and front slashes interchangeably so a path could look like "c:\\Dir1/Dir2//Dir3\filename.txt" Typically, I wouldn't worry too much about this but I need ability to re-map any given path. So the code would take old Path and New path as parameters and will have to change any record in the table from old path to the new. So in this example input parameters might be "c:\Dir1", "\\Server\Share" Now I need to find every permutation of "c:\Dir1" in the table including "c:\Dir1", "c:\\Dir1", "c:/Dir1", "c://Dir1", etc...

Any ideas?

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-01-24 : 13:07:36
If the variations are known, you should be able to use two (or more) nested replaces to get it to your desired format - for example like shown below
-- see if the path exists and what the fixed up path would be
SELECT dirpath, replace(replace(dirpath,'/','\'),'\\','\') FROM theTable;

-- replace with the new path
declare @newPath varchar(255)='D:\Dir1';
declare @oldPath varchar(255)='C:\Dir1';
UPDATE theTable SET
dirpath = @newPath
where
replace(replace(dirpath,'/','\'),'\\','\') = @oldPath ;
Go to Top of Page
   

- Advertisement -