Wednesday, April 5, 2017

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 method is here 



 /*

 PRINT STAR TRIANGLE IN SQL

 */

 DECLARE @Limit     INT =5
 DECLARE @Iteration INT =1

 WHILE (0<@Limit)
  BEGIN
   PRINT SPACE(@Limit-1)+REPLICATE('*',@Iteration)
   SELECT @Limit=@Limit-1
   SET @Iteration=@Iteration+2
 END


OUT PUT :


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




 

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




 


 

 

Tuesday, April 4, 2017

How to reverse a string in MS-Sql Server without using REVERSE() function

How to reverse a string in MS-Sql Server without using REVERSE( ) function

    Its simple ,Try following query  
its one of the interview question in these days

DECLARE @Str VARCHAR (50)='MS SQL'
DECLARE @Result VARCHAR (50)=''
DECLARE @Len INT

SET @Len=LEN (@Str)
               
           WHILE (0<@Len)
           BEGIN
                 SET @Result=@Result+SUBSTRING (@Str,@Len,1)
                 SET @Len=@Len-1
          END

PRINT @Result



OUT PUT :




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...