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.

+3


source to share


9 replies


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,'*')
--------------------------------------------------------------------------------
*
* *
* * *

      

+7


source


Not sure what exactly you are looking for. Perhaps it?

select '*' from dual
union all select '**' from dual
union all select '***' from dual

      



Example

+2


source


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

      

+1


source


If you want a simple triangle, you can do this:

SELECT '*' FROM table
UNION
SELECT '**' FROM table
UNION
SELECT '***' FROM table

      

0


source


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

      

0


source


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

-1


source


declare @row int = 5,
@index int = 0,
@string nvarchar(5) =''
while @row > 0
begin
    set @index = @row
    while @index > 0
    begin
        set @string = '*' + @string
        set @index = @index - 1
    end
    print @string
    set @string = ''
    set @row = @row - 1
end

      

 *****
 ****
 ***
 **
 *

      

-1


source


DECLARE @lclMaxLevel INT=5
DECLARE @lclPrintCount INT =0

WHILE @lclMaxLevel > 0
  BEGIN
      PRINT Space(@lclMaxLevel)
            + Replicate('*', @lclPrintCount+1)

      SET @lclMaxLevel=@lclMaxLevel - 1
      SET @lclPrintCount=@lclPrintCount + 1
  END

      

-1


source


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)

      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * * 

      




-2


source







All Articles