1

I have a varchar(max) column with HTML present and I need to replace a single character where it is between a line break - <br></br> eg: <br>8</br>. The problem is, that character is entirely random so I need the equivalent of a wildcard replace of <br>%</br>. I only need it to happen if it is a single character, not if it is multiple characters.

So if its <br>#</br> i want to change it to <br></br>

Any ideas how to achieve this? Function / View / Stored procedure any method will do.

Thanks

SkipFeeney
  • 61
  • 1
  • 6

2 Answers2

0

You could try using replace. Not an expert in SQL server, but it's usually Replace(texttosearch, paterntofind, newpattern)

Replace(Yourcolumn, '<br>.</br>', '<br></br>')

It's unclear if there's other text you want to preserve or not. You may need some capture groups if there is.

Serban Tanasa
  • 3,592
  • 2
  • 23
  • 45
0

I will do this using STUFF and CHARINDEX

Charindex - Helps you identify the position of <br>

Stuff - Helps you to replace the character with empty string

SELECT Stuff(strings, Charindex('<br>', strings) + 4, 1, '') as Result
FROM   (VALUES ('<br>8</br>'),
               ('<br>.</br>'),
               ('<br>#</br>'),
               ('<br>$</br>')) tc (strings) 

Result:

Result
------
<br></br>
<br></br>
<br></br>
<br></br>
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172