1) Change your variable
DECLARE @dataToWrite VARCHAR(MAX)
2) Add an IF condition
IF LEN(@brand) < 38 BEGIN
SET @dataToWrite = @dataToWrite + @xmlData
SET @dataToWrite = REPLACE(REPLACE(@dataToWrite, '@item1@', @brand), '@item2@', @total)
SET @dataToWrite = REPLACE(@dataToWrite, '@item3@', REPLACE( @brand, ' ', '+'))
END
I attached the modified script.