Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 734641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:23:47+00:00 2026-05-14T07:23:47+00:00

I have a table in SQL Server 2005 with hundreds of rows with HTML

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:23:47+00:00Added an answer on May 14, 2026 at 7:23 am

    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:

    Select ..
    From ...
    Where HtmlContent Like '<span class="heading-2">%'
    

    This assumes no additional spacing between span and class as 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 find div tags marked as heading-2 in 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:

    <span class="heading-2"> Foo <span class="heading-3">Bar</span> And Gamma Too</span> .... <span class="heading-4">Fubar Is the right way!</span>...
    

    It is not so simple to find the correct closing span tag to change to </h2>. You cannot simply find the first </span> and change it to </h2>. If you knew that you had no nested span tags, then you could write a user-defined function that would do it:

    Create Function ReplaceSpanToH2( @HtmlContent nvarchar(max) )
    Returns nvarchar(max)
    As
    Begin
        Declare @StartPos int
        Declare @EndBracket int
    
        Set @StartPos = CharIndex('<span class="heading-2">', @HtmlContent)
        If @StartPos = 0
            Return @HtmlContent
    
        Set @HtmlContent = Replace(@HtmlContent, '<span class="heading-2">', '<h2>')
    
        -- find next </span>
        Set @StartPos = CharIndex('</span>', @HtmlContent, @StartPos)
    
        Set @HtmlContent = Stuff(@HtmlContent, @StartPos, 7, '</h2>')
        Return @HtmlContent
    End
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in SQL Server 2005 which has a date time field.
I'm using SQL-Server 2005. I have table with many columns, rows i select have
In SQL Server 2005 I have a table with data that looks something like
I have a table in SQL Server 2005 which contains some changelog data for
I have a table in SQL Server 2005 which has three columns: id (int),
I have a table in a SQL Server 2005 database that logs purchases like
I have a table in SQL Server 2005 database that has following columns: Id,ProductName,Year,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
We have a table like below in an sql server 2005 db: event_id staff_id
I am using SQL server 2005. I have a table like this - ID
I have a table in SQL Server 2005. alt text http://www.techpint.com/sites/default/files/images/table.JPG I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.