We help IT Professionals succeed at work.

Use REPLACE function with like operator

50 Views
Last Modified: 2015-08-31
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?
Comment
Watch Question

Jim HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015

Commented:
Give us a before and after data mockup of what you're trying to pull off here...

Author

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.

BEFORE

<text>H:PATH1\PATH2\PATH3<text>

AFTER

<text>Z:\ABC\PATH1<text>
Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
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

Author

Commented:
I get the following error:

Argument data type text is invalid for argument 1 of replace function.
Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
The field is the column name of your table . . . as we don't know your schema the answer needed to be generic.

Author

Commented:
I understand.  I put the column name in and received the above error.
Kyle AbrahamsSenior .Net Developer
CERTIFIED EXPERT

Commented:
What is the field type for the column (EG: Varchar, xml?)

Author

Commented:
It is text.  Do I need to convert the field?
Senior .Net Developer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.