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

  • SEARCH
  • Home
  • 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 6727347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:02:21+00:00 2026-05-26T10:02:21+00:00

I am using SSMS 2008 and am trying to write a recursive replace statement.

  • 0

I am using SSMS 2008 and am trying to write a recursive replace statement. I have a good start on this, but it is not working fully yet. I want to replace every occurrence of XML tags occurring in one column with empty string. So I want to replace the whole range from “<” to “>” for each record. Here is what I have:

DECLARE @I INTEGER
SET @I = 3
while 
@I > 0
--(select [note_text] from #TEMP_PN where [note_text] LIKE '%<%') 
BEGIN
UPDATE #TEMP_PN 
SET [note_text] = replace([note_text],substring([note_text],CHARINDEX('<',[note_text]),CHARINDEX('>',[note_text])),'')
from #TEMP_PN
where [note_text] LIKE '%Microsoft-com%'
SET @I = @I - 1 
END

SELECT * FROM #TEMP_PN

The problem with this code is I hardcoded @I to be 3. However, I want to make it continue replacing from “<” to “>” with empty string for each record until there are no more “<” chars. So I tried the commented out line above but this gives me an error on more than one record / subquery. How can I achieve this recursive functionality? Also, my Replace statement above only replaced “<” chars for some records, strangely enough.

I tried your sample code, but it still does not replace all instances of this text per record and for some records it does not replace any text although there is “<” in these records. Here is a record where your script does not replace any substrings. Maybe this is a special character problem?

<DIV class=gc-message-sms-row><SPAN class=gc-message-sms-from>TLS: </SPAN><SPAN class=gc-message-sms-text>Hi Reggie... I'm on my way to Lynn.. see you soon</SPAN> <SPAN class=gc-message-sms-time>3:09 PM </SPAN></DIV>
  • 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-26T10:02:21+00:00Added an answer on May 26, 2026 at 10:02 am

    You were pretty close… the problem is that the SUBSTRING‘s third parameter is a length, not the position to stop at.

    DECLARE @RowsUpdated INT
    SET @RowsUpdated = 1
    WHILE (@RowsUpdated > 0)
    BEGIN
        UPDATE #TEMP_PN 
    SET [note_text] = 
        REPLACE(
                [note_text], 
                substring(
                    [note_text],
                    CHARINDEX('<',[note_text]), 
                    CHARINDEX(
                        '>',
                        SUBSTRING([note_text], CHARINDEX('<',[note_text]), 1 + LEN([note_text]) - CHARINDEX('<',[note_text]))
                    )
                ),
            '')
    from #TEMP_PN
    where [note_text] LIKE '%Microsoft-com%' and [note_text] like '%<%>%'
    
        SET @RowsUpdated = @@ROWCOUNT
    END
    
    SELECT * FROM #TEMP_PN
    

    SECOND EDIT:

    OK, I’ve updated both queries; this code should now handle the leading > before the first tag… which I think could have been the issue.

    DECLARE @TestString VARCHAR(MAX)
    SELECT @TestString = '><DIV class=gc-message-sms-row><SPAN class=gc-message-sms-from>TLS: </SPAN><SPAN class=gc-message-sms-text>Hi Reggie... I''m on my way to Lynn.. see you soon</SPAN> <SPAN class=gc-message-sms-time>3:09 PM </SPAN></DIV>'
    
    DECLARE @RowsUpdated INT
    SET @RowsUpdated = 1
    
    WHILE (@RowsUpdated > 0)
    BEGIN
    
        SELECT
            @TestString = 
                REPLACE(
                    @TestString, 
                    substring(
                        @TestString,
                        CHARINDEX('<',@TestString), 
                        CHARINDEX(
                            '>',
                            SUBSTRING(@TestString, CHARINDEX('<',@TestString), 1 + LEN(@TestString) - CHARINDEX('<',@TestString))
                        )
                    ),
                '')
        WHERE @TestString LIKE '%<%>%'
    
        SET @RowsUpdated = @@ROWCOUNT
    END
    
    SELECT @TestString
    

    Could it be because that note doesn’t meet the [note_text] LIKE '%Microsoft-com%' criteria?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SSMS 2008 and trying to insert with this query but am
I am using SSMS 2008 and trying to use a HAVING statement. This should
This should be really simple. I am using SSMS 2008, trying to get a
I am using SSMS 2008 and trying to concatenate one of the rows together
I am using SSMS 2008 and am trying to select count of consumers who
I am using SSMS 2008, trying to select just one row/client. I need to
I am using SSMS 2008 and I have the following Scalar function to take
I am using SSMS 2008 and I have the following scalar function to take
How to save query result using T-SQL in a csv file in SSMS 2008?
With SQL Server 2005 using SSMS, when I run this: SELECT * FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','Excel

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.