Thursday, December 26, 2019

Xmas tree in T-SQL

This is a very simple version of a Xmas tree using T-SQL, also is a good example to learn how recursive CTE works.

So, if you are in front of the computer with little work to do, take a look at the code, maybe you can improve it further!


/* Draw a simple xmas tree using recursion 
   🎄 Happy holidays!              */
WITH XmasTree
AS (
 SELECT 
  CAST(REPLICATE(' ', 16) + '^' AS VARCHAR(50)) AS t,
  0 AS lvl
 UNION ALL
 SELECT 
  CAST(REPLICATE(' ', 15 - lvl) + '/' + REPLICATE('*', 2 * lvl + 1) + '\' AS VARCHAR(50))
  ,n.lvl + 1
 FROM XmasTree n
 WHERE lvl < 16
 )
SELECT t as [Happy Holidays]
FROM XmasTree;

And this is the final result:



HAPPY HOLIDAYS!

No comments:

Post a Comment

Data Engineering Books Worth Having on Your Shelf (or your tablet)

Good documentation gets you started. Good books get you deep. After years of working with cloud data platforms, SQL engines, and m...