I want to split a string into groups of rows having lengths NOT more than 30, but while splitting, the last word should not break, if it breaks, move that word to the next row set.
Following is the sample string:
DECLARE @FullAddress VARCHAR(MAX)= '216 Apartment123 AreaArea SampleWord1 Word2 MiddleTown Upper1Location Another5 NewYork'
Required Output:
+-----+------------------------------+
| Row | Result |
+-----+------------------------------+
| 1 | 216 Apartment123 AreaArea |
| 2 | SampleWord1 Word2 MiddleTown |
| 3 | Upper1Location Another5 |
| 4 | NewYork |
+-----+------------------------------+
I tried with substring, but it breaks the word at a fixed length
DECLARE @FullAddress VARCHAR(MAX)= '216 Apartment123 AreaArea SampleWord1 Word2 MiddleTown UPLocation Downtownocation NewYork'
SELECT SUBSTRING(@FullAddress,1,30)
UNION ALL
SELECT SUBSTRING(@FullAddress,31,30)
UNION ALL
SELECT SUBSTRING(@FullAddress,61,30)