Sum It Up with a Digital Triangle

0

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

DECLARE @ varchar(100)= '1 2 3 4 5 6 7'

DECLARE @j INT=1,@i INT=LEN(@)
WHILE @i>0
BEGIN
PRINT @
  WHILE @i>@j
    SELECT @=STUFF(@,@i-1,2,RIGHT(SUBSTRING(@,@i-2,1)+SUBSTRING(@,@i,1)*1,1)+' '),@i-=2
  SELECT @=STUFF(@,@j,1,' '),@j+=1,@i=LEN(@)
END

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results