Replace HTML Tags

  • Hi all!

    I have a varchar(max) field where I store HTML documents. I'm planning to add a custom tag to all the documents for later use to update the "CompanyName".

    I plan to put some tags in the documents where the company name will be, somthing like: "<g:cn> The ComplanyName </g:cn>"

    Later when I need to use the documents for an other company, I can replace the text between the tags.

    So my question is : are there a SQL function that can replace text inside a such tags? I have spent several hours searching the net, but I could find nothing useful.

    Atle

  • This should get you started:

    Declare @html varchar(max);

    Set @html = '<g:cn> The ComplanyName </g:cn>';

    Select

    @html,

    Stuff(@html, Patindex('%<g:cn>%', @html) + 6, Patindex('%</g:cn>%', @html) - (Patindex('%<g:cn>%', @html) + 6), 'New CompanyName')

    PatIndex finds the start location of a specific pattern within a string. Stuff replaces any characters at the given start position fro the given length. For more details look up the functions in BOL.

  • Fanstatic!

    Thank you Jack, I didn't think about solving it that easy. I will use this.

    Regards

    Atle

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply