Heres my first take, though Id like to see this done using a set based approach. Ill keep at it.
Interesting problem!
declare @Txt varchar(max),
@Limit int
set @Txt = 'This is a long sentence and I want to split it into multiple sentences of 25 character each with out splitting a word into multiple lines'
set @Limit = 25
;with cte_DoIt
as ( select substring(@Txt, 0, ((@Limit+1)-charindex(' ', reverse(left(@Txt + ' ',@Limit))))) [Chunk],
stuff(@Txt, 1, ((@Limit+1)-charindex(' ', reverse(left(@Txt + ' ',@Limit)))), '') [Swap]
union all
select substring(Swap, 0, (@Limit+1) - charindex(' ', reverse(left(Swap + ' ', @Limit)))),
stuff(Swap, 1, ((@Limit+1)-charindex(' ',reverse(left(Swap + ' ',@Limit)))), '')
from cte_DoIt
where len(Swap) > 0
)
select Chunk, len(Chunk) [length]
from cte_DoIt