Replacing text within a single column, but not the entire string

  • Hello Everyone

    I am working on trying to become better with some of the string functions in SQL. I was working on a simple project, but saw an opportunity to use some of the data to try something.

    Say that I have a string as this:

    This is what needs to remain <P>replace this with different</p> and this will remain

    All the columns have this same format, although the length and text will be different. All rows have the opening and closing tags of

    <p> text in between</p>

    DECLARE @Text Nvarchar(250)

    DECLARE @StartPoint int

    DECLARE @EndPoint int

    SET @Text = 'This is what needs to remain <P>replace this with different</p> and this will remain'

    SET @StartPoint = CHARINDEX('<P>', @Text)

    SET @EndPoint = CHARINDEX('</p>', @Text, @StartPoint+2)

    SELECT @StartPoint, @EndPoint

    What I would like to do is replace Only the text that is inside of the Paragraph tags, and leave all the rest alone.

    The code that I have so far will return the integer space values of where each tag begins.

    What can I use to determine how to replace the text? I have the beginning and ending values.

    This is not homework, I am a little too old to be in school. Just trying to better myself, while things are a little slow at work

    Thanks in advance

    Andrew SQLDBA

  • Look at the STUFF function in BOL.

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • The STUFF function was perfect. Did exactly what I was after.

    Thanks

    Andrew SQLDBA

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

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