Friday, March 9, 2012

Repalce partial string in table

Hi! I need to replace a string in a SQL table with a new path.

Essentially, I want to find and replace the share path while retaining the rest of the path past that point.

I tried using Update (on a test table of course) but it sompley find and replaces with no regards to the rest of the path.

Any help is greatly appreciated!

JJDDL and some sample data would be helpful|||http://www.apcit.com/work/john.gif

I need to replace a portion of the path in the source column with a new vaule but still retain the rest of the text.

Hope this helps!

JJ|||DECLARE @.Path varchar(255), @.find varchar(25), @.replace varchar(25)

SELECT @.Path = 'C:\myTemp99\Brett\Folders\',@.find = 'Brett', @.replace = 'JJ'

SELECT @.Path = REPLACE(@.Path,@.find,@.replace)

SELECT @.Path|||Brett,

Thanks for your reply but how do I specify a table within the database?

For clarity:

I want to open a table, for a given object in the table I want to replace a portion of the value with a new one and append the rest of the data to the end of the new value.

www.apcit.com/work/john.gif

Essentially the path to the SOURCE has changed and I just want to find and repalce.

Thanks again!|||I'd try something like:UPDATE junque
SET pathname = Replace(pathname, '/work/', '/play/')
WHERE 0 < CharIndex(pathname, '/work/')Do check it out on a scratch table first though, this could cause havok if misused!

-PatP|||I'm actually using SQL query analyzer to run these, so please use that syntax.

Trying to resolve the @.PATH var specified in the earlier post. I'm formatting it something like this:

@.Path = 'sms_xxx.dbo.smspackages.source'

Is this correct? Basically sms_xxx is the DB. smspackages is the table and source is the column I want to find and replace the value in.

Any help is golden!

Thanks!

JJ|||Originally posted by jjrenner
I'm actually using SQL query analyzer to run these, so please use that syntax.

Trying to resolve the @.PATH var specified in the earlier post. I'm formatting it something like this:

@.Path = 'sms_xxx.dbo.smspackages.source'

Is this correct? Basically sms_xxx is the DB. smspackages is the table and source is the column I want to find and replace the value in.

Any help is golden!

Thanks!

JJ These all seem to be acceptable syntax for SQL Query Analyzer.

The @.path in Brett's example was a declared variable that he was using to illustrate the concept.

I'd suggest that you use something vaguely like:UPDATE sms_xxx.dbo.smspackages
SET source = Replace(source, 'original', 'new')-PatP|||Pat,

Worked like a charm. Thanks for the baby steps!

JJ

No comments:

Post a Comment