Use REPLACE function with like operator

I would like to use the REPLACE function to replace specific text in a field.  I only need to replace that portion of identified text in the field and leave the remaining text before and after the replace intact.  How do I go about achieving this?  I assume the LIKE operator is needed in some fashion?
Gary HarperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Data DudeCommented:
Give us a before and after data mockup of what you're trying to pull off here...
Gary HarperAuthor Commented:
In the example below would like to change the path information where field LIKE %H:PATH1\PATH2\PATH3% to %Z:\ABC\PATH1%.  There is text before and after this value.




Kyle AbrahamsSenior .Net DeveloperCommented:
you can use a charindex instead:

--update table set <field> = replace(<field>, 'h:PATH1\PATH2\PATH3', 'z:\abc\path1') 
select <field> as old, replace(<field>, 'h:PATH1\PATH2\PATH3', 'z:\abc\path1') as new
from table
where charindex( 'h:PATH1\PATH2\PATH3',<field>) > 0

Open in new window

10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Gary HarperAuthor Commented:
I get the following error:

Argument data type text is invalid for argument 1 of replace function.
Kyle AbrahamsSenior .Net DeveloperCommented:
The field is the column name of your table . . . as we don't know your schema the answer needed to be generic.
Gary HarperAuthor Commented:
I understand.  I put the column name in and received the above error.
Kyle AbrahamsSenior .Net DeveloperCommented:
What is the field type for the column (EG: Varchar, xml?)
Gary HarperAuthor Commented:
It is text.  Do I need to convert the field?
Kyle AbrahamsSenior .Net DeveloperCommented:
Yeah, do a cast to nvarchar(max).

Note that if your text is over 8000 chars there may be a truncation . . . in which case you would use:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.