Solution to TSQL Beginners Challenge 15
;with cte
as
(
select id,Sentence as OrginalSentence,Sentence+' ' as loopsentence,
Convert(varchar(1000),'') as ReversedSentence from @t
union all
select c.ID,OrginalSentence,
SUBSTRING(loopsentence,charindex(' ',loopsentence,1)+1,LEN(loopsentence)),
Convert(varchar(1000),SUBSTRING(loopsentence,1,charindex(' ',loopsentence,1)-1)+' '+ReversedSentence)
from cte c
where CHARINDEX(' ',loopsentence,1)<>0
)select distinct id,OrginalSentence,ReversedSentence as ReversedSentence From cte where loopsentence=''
Tags: