Tags

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,


SQL Puzzle | The Star Pattern Puzzle

Puzzle Statement

In this puzzle you got to print following patterns without using loop or cursor. You are not going to get this kind of thing in your projects. This is only for fun.

The patterns we are looking for are –

Pawan Kumar Khowal - Print Pattern

Pawan Kumar Khowal – Print Pattern

Rules/Restrictions

The solution should be should use “SELECT” statement or “CTE”.
Add your solution(s) in the comments section or send you solution(s) to pawankkmr@gmail.com

SOLUTION 1 | USING Numbers Table & SELECT

--

SELECT REPLICATE(Star,number) PrintPattern FROM ( SELECT '*' Star ) b CROSS APPLY (
SELECT DISTINCT number
FROM 
	MASTER..SPT_VALUES
WHERE number > 0 and number < 10 AND number % 2 = 1 ) a 

SELECT RIGHT(CONCAT(REPLICATE(' ',9) , REPLICATE(Star,number)),9) 
PrintPattern 
FROM ( SELECT '*' Star ) b CROSS APPLY ( 
SELECT DISTINCT number FROM MASTER..SPT_VALUES 
WHERE number > 0 and number < 10
AND number % 2 = 1 ) a

--
Pawan Kumar Khowal - Print Pattern Outputs

Pawan Kumar Khowal – Print Pattern Outputs

Add a comment if you have any other solution in mind. I would love to learn it. We all need to learn.

Enjoy !!! Keep Learning

Pawan Khowal 

Http://MSBISkills.com

Advertisements