I am trying to change the following script using a WITH statement that is supposed to split up a string based on length I specify (size) and position of spaces. I changed the with to just declare the table instead however the output I get is not the same, the original code is -DECLARE @String VARCHAR(MAX), @Size TINYINTSELECT @String = 'Hello my name is Jeff. I need some help on a project because ', @String = @String + 'right now this is how the application i am working ', @String = @String + 'with displays data.', @Size = 45 ;WITH Peso (Part, StartPos, Size) AS ( SELECT 1, 1, CAST(@Size - CHARINDEX(' ', REVERSE(LEFT(SUBSTRING(@String, 1, @Size) + ' ', @Size))) AS INT)UNION ALL SELECT Part + 1, StartPos + Size + 1, CAST(@Size - CHARINDEX(' ', REVERSE(LEFT(SUBSTRING(@String, StartPos + Size + 1, @Size) + ' ', @Size))) AS INT) FROM Peso WHERE StartPos + Size <= DATALENGTH(@String) ) SELECT Part, SUBSTRING(@String, StartPos, Size) FROM Peso ORDER BY Part
Which outputs - Part ---- -------------------------------- 1 Hello my name is Jeff. I need 2 some help on a project because 3 right now this is how the 4 application i am working with 5 displays data.And I have tried to modify my code just to create the table and do the same thing like so - DECLARE @String VARCHAR(200)DECLARE @Size intSELECT @String = 'Hello my name is Jeff. I need some help on a project because ', @String = @String + 'right now this is how the application i am working ', @String = @String + 'with displays data.', @Size = 45DECLARE @Peso TABLE (Part int, StartPos int, Size int)INSERT INTO @Peso SELECT 1, 1, CAST(@Size - CHARINDEX(' ', REVERSE(LEFT(SUBSTRING(@String, 1, @Size) + ' ', @Size))) AS INT)UNION ALL SELECT Part + 1, StartPos + Size + 1, CAST(@Size - CHARINDEX(' ', REVERSE(LEFT(SUBSTRING(@String, StartPos + Size + 1, @Size) + ' ', @Size))) AS INT) FROM @Peso WHERE StartPos + Size <= DATALENGTH(@String) SELECT Part, SUBSTRING(@String, StartPos, Size) FROM @Peso ORDER BY Part
Except now my output is - 1 Hello my name is Jeff. I need some help on a2 project because right now this is how theand I lose the rest of the lines. Any help on how to change this like the first one but in SQL 2000 so I get all the lines greatly appreciated!