I have a table in SQL Server 2005 with hundreds of rows with HTML content. Some of the content has HTML like:
<span class=heading-2>Directions</span>
where “Directions” changes depending on page name.
I need to change all the <span class=heading-2> and </span> tags to <h2> and </h2> tags.
I wrote this query to do content changes in the past, but it doesn’t work for my current problem because of the ending HTML tag:
Update ContentManager
Set ContentManager.Content = replace(Cast(ContentManager.Content AS NVARCHAR(Max)), 'old text', 'new text')
Does anyone know how I could accomplish the span to h2 replacing purely in T-SQL? Everything I found showed I would have to do CLR integration. Thanks!
Indeed T-SQL does not natively support regular expressions and this is the sort of problem in which regular expressions would be the tool of choice. First, I’ll say that the level of complication in the solution depends greatly on how consistent your data is. For example, suppose we search for items with the heading:
This assumes no additional spacing between
spanandclassas well as no additional spacing after the final double quote before the end bracket. We could write'%<span%class="heading-2"%>%'to account for the spaces but that would also finddivtags marked asheading-2in the same content as any span tag. If this later scenario shouldn’t happen but you might have varying spaces, then use this revised pattern. Where we will really run into troubles is the closing tag. Suppose our content looks like so:It is not so simple to find the correct closing
spantag to change to</h2>. You cannot simply find the first</span>and change it to</h2>. If you knew that you had no nestedspantags, then you could write a user-defined function that would do it: