Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP! Problems with REPLACE and SUBSTRING!

Status
Not open for further replies.

questhaven

Programmer
Mar 28, 2001
81
0
0
US
Hi there - I am trying to create a system in which I can pass "tokens" within a string and then parse them out within a stored procedure and replace them with alternate words. Using the code I wrote below (I am just running this is in query analyzer so far), I can get the first "token" to replace perfectly fine, however, when I continue on to search for a second token, it not only replaces that, but the entire rest of the sentence as well. Could someone take a look and see if they notice any mistakes I may be making? THANKS!!

DECLARE @STRING VARCHAR(100), @STRING2 VARCHAR(100), @NEWSTRING VARCHAR(100)
DECLARE @STARTINGPOINT INT, @ENDINGPOINT INT
SELECT @STRING = '<<hello>> my name is michele. This <<obj>> is a <<test>> page.'
print(@string)
SELECT @STARTINGPOINT = CHARINDEX('<', @String)
SELECT @ENDINGPOINT = CHARINDEX('>', @String)+1
SELECT @NEWSTRING = SUBSTRING(@STRING, @STARTINGPOINT, @ENDINGPOINT )

select @string2 = replace(@STRING, @NEWSTRING, 'Welcome,')
print(@string2)
if (CHARINDEX('<', @STRING2) > 0) and (CHARINDEX('>', @STRING2) > 0)
BEGIN

SELECT @STARTINGPOINT = CHARINDEX('<', @String2)
SELECT @ENDINGPOINT = CHARINDEX('>', @String2)+1

SELECT @NEWSTRING = SUBSTRING(@STRING2, @STARTINGPOINT, @ENDINGPOINT)

select @string3 = replace(@STRING2, @NEWSTRING, 'page')
print(@string3)
END
else
BEGIN
print('no more tokens')
END

- The reason why I am doing this with a stored procedure, as opposed to something like VBScript, is because I am eventually going to be replacing the words with content from the database - and I also need to have this be portable across different types of applications.
 
Nevermind - I figured it out. The third parameter of Substring is Length. I guess it was working on the first &quot;token&quot; because it was the first item in the string.

Thanks anyway!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top