0

I'm using SQL Server 2008 and need to strip out quite a bit of data within a string. Because of the nature and variability of the string, I think I'm needing to use multiple, nested REPLACE commands. The problem is each REPLACE needs to build on the previous one. Here is a sample of what I'm looking at:

<Paragraph><Replacement Id="40B"><Le><Run Foreground="#FFFF0000">Treatment by </Run></Le><Op isFreeText="True"><Run Foreground="#FFFF0000">test</Run></Op><Tr><Run Foreground="#FFFF0000">.  </Run></Tr></Replacement></Paragraph>

Essentially, I need it to return just the text outside of the <> brackets so for this example it would be:

Treatment by test.  

Also, I wanted to mention that the strings inside the <> brackets can vary quite a bit for each row both by content and length, but it isn't relevant for what I'm needing other than making it more complex for replacing.

Here is what I've tried:

REPLACE(note,substring(note,patindex('<%>',note),CHARINDEX('>',note) - CHARINDEX('<',note) + 1),'')

And it returns:

<Replacement Id="40B"><Le><Run Foreground="#FFFF0000">Treatment by </Run></Le><Op isFreeText="True"><Run Foreground="#FFFF0000">test</Run></Op><Tr><Run Foreground="#FFFF0000">.  </Run></Tr></Replacement></Paragraph>

Somehow I need to keep going with replacing each of the <> brackets but don't know how to proceed. Any help or guidance would be greatly appreciated!!!

anothermh
  • 9,815
  • 3
  • 33
  • 52
Rob
  • 3
  • 1

1 Answers1

0

Depending on how you have that string holding the HTML fragment available you could try to use something like:

SELECT convert(xml, '<Paragraph><Replacement Id="40B"><Le><Run Foreground="#FFFF0000">Treatment by </Run></Le><Op isFreeText="True"><Run Foreground="#FFFF0000">test</Run></Op><Tr><Run Foreground="#FFFF0000">.  </Run></Tr></Replacement></Paragraph>').value('/', 'varchar(255)') as stripped

You convert it to XML and then use the built in xml parser function "value".

itsLex
  • 786
  • 1
  • 5
  • 13
  • That did it! Thank you so much! Now I just need to figure out what all of that means! – Rob Nov 13 '17 at 18:36