Wednesday, April 5, 2017

How to Print Number Triangle/Pyramid in SQL

Print Number Triangle In Following Format

     1

   121

 12321 

You can use following query for print number triangle .



/*
TO PRINT NUMBER PYRAMID
*/
DECLARE @Limit INT =9
DECLARE @Number INT =0
DECLARE @Result VARCHAR(20)=''
WHILE (0<@Limit)
 BEGIN
   SET @Number=@Number+1
   SET @Result= SPACE(@Limit-1)+LTRIM(@Result)+CAST (@Number AS VARCHAR)
   PRINT @Result+ REVERSE(SUBSTRING (@Result,1,LEN(@RE)-1))
   SET @Limit=@Limit-1
 END 


OUT PUT :

          1       
        121      
       12321     
      1234321    
     123454321   
    12345654321  
   1234567654321 
  123456787654321
12345678987654321




 


 

 

No comments:

Post a Comment

Comments system

Disqus Shortname

How To Print Star Triangle In SQL Server Using REPLICATE( ) Function

Print * Triangle In SQL Server Using REPLICATE( ) Function   In SQL Server we can print * triangle in many methods.One of the simple m...