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?
Microsoft SQL Server 2008
Last Comment
Kyle Abrahams
8/22/2022 - Mon
Jim Horn
Give us a before and after data mockup of what you're trying to pull off here...
Gary Harper
ASKER
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 Abrahams
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 newfrom tablewhere charindex( 'h:PATH1\PATH2\PATH3',<field>) > 0