You can use REPLICATE to repeat a character a certain number of times. To generate a sequence of numbers from 1 to 20 you don't need a WHILE anyway - SQL doesn't really need the WHILE statement to work with data.
Number sequences are always useful which is why almost every SQL developer creates a Numbers table.
If you don't already have one, a quick and dirty way to generate 20 numbers is to select the top 20 rows from a systems table, and use ROW_NUMBER to calculate row numbers eg:
select top 20 replicate('*',21-row_number() over (order by id) )
from sys.sysobjects
With a Numbers table, the query is simpler:
select replicate('*',Number )
from dbo.Numbers
where Numbers.Number <= 20
order by Number desc
Numbers tables are extremely useful, eg for sets of elements like 200 days starting from 2017/1/1 :
select dateadd(d,Number,cast('20170101' as date))
from dbo.Numbers
where Numbers.n<= 20
order by Number desc
Try This,
Try the following code:
You can see more different triangle examples in here
You can use
REPLICATE
to repeat a character a certain number of times. To generate a sequence of numbers from 1 to 20 you don't need aWHILE
anyway - SQL doesn't really need theWHILE
statement to work with data.Number sequences are always useful which is why almost every SQL developer creates a Numbers table.
If you don't already have one, a quick and dirty way to generate 20 numbers is to select the top 20 rows from a systems table, and use
ROW_NUMBER
to calculate row numbers eg:With a Numbers table, the query is simpler:
Numbers tables are extremely useful, eg for sets of elements like 200 days starting from 2017/1/1 :
Instead of a table, values(),(),.. can be used.
Cross join of
and
will result in
Taking this into account, a query like below can be built:
Use REPLICATE inside a WHILE. I think, you can achieve your desired output if you do it correctly?