questhaven
Programmer
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.
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.