How to print triangle of stars using SQL
Is it possible to create a triangle of stars like this below in SQL. I know it can be done easily in any other programming language like C, C ++, Java, but want to know if it is really possible with just SQL or PL / SQL.I tried working with it with a double table in Oracle but couldn't get through it.
* *
* * * *
* * * or * * *
Maybe someone will spill if anyone knows about it.
source to share
The simplest approach is something like this. You can get more complex, especially if you want to build an equilateral triangle rather than a right triangle.
SQL> ed
Wrote file afiedt.buf
1 select rpad( '* ', level*2, '* ' )
2 from dual
3* connect by level <= 3
SQL> /
RPAD('*',LEVEL*2,'*')
--------------------------------------------------------------------------------
*
* *
* * *
source to share
Try it.
declare @x int,@y int,@diff int
select @x=0,@y=10,@diff=2--diferrence between consecutive rows
while @x<@y
begin
if @x=0 and @diff<>1
print space((@y-@x)*@diff-1)+replicate('*',1)
else if(@diff%2=0)
print space((@y-@x)*@diff)+replicate('* ',@x+(@x*(@diff-1)))
else
print space((@y-@x)*@diff)+replicate('* ',@x+(@x*(@diff-1)))
select @x=@x+1
end
source to share
declare @count int,@num int,@num1 int, @space int, @str varchar(50)
set @count = 5 set @num = 1
while(@num<=@count)
begin
set @num1 = 0 set @space = @count-@num
while (@num1<@num)
begin
if @str is null
set @str = '* '
else
set @str = @str+'* ' set @num1 = @num1+1
end
print (space(@space)+@str)
set @num = @num+1 set @str = null
end
source to share
Here is a script to get a perfect triangle or pyramid in sql (tested in Microsoft Sql 2008)
declare @x int,@y int
select @x=5,@y=0
while @x>0
begin
print space(@x)+replicate('*',@y)+replicate('*',@y+1)
set @y=@y+1
set @x=@x-1
end
*
***
*****
*******
*********
you can get many more scripts and help from this link ... it was helpful for me
Ref: - sqlquerynscript
source to share
select rpad ('*', level * 2, '*') from double join at level <= 10
select rpad ('', r * 2, '') || rpad ('*', l * 2, '*') k from (select level l, row_number () over (order by null) r from double join by level <= 10 order by l desc)
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
select rpad ('', l * 2, '') || rpad ('*', r * 2, '*') k from (select level l, row_number () over (order by null) r from double join by level <= 10 order by l desc)
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
select rpad ('', l, '') || rpad ('*', r * 2, '*') k from (select level l, row_number () over (order by null) r from double join by level <= 10 order by l desc)
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
source to share