Created
May 2, 2022 20:45
-
-
Save jingyang-li/c6dcac50aa363d011de33df1371919e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @myFizzBuzz varchar(max) | |
;WITH Num1 (n) AS ( | |
SELECT 1 as n | |
UNION ALL SELECT n+1 as n | |
FROM Num1 Where n <101), | |
Num2 (n) AS (SELECT 1 FROM Num1 AS X, Num1 AS Y), | |
Nums (n) AS (SELECT ROW_NUMBER() OVER(ORDER BY n) FROM Num2) | |
Select @myFizzBuzz = | |
( | |
STUFF( | |
(SELECT char(9)+char(10)+ | |
CASE WHEN n % 15 = 0 THEN 'FizzBuzz' | |
WHEN n % 5 = 0 THEN 'Buzz' | |
WHEN n % 3 = 0 THEN 'Fizz' | |
ELSE CAST(n AS VARCHAR(8)) END | |
FROM Nums Where n<1000 | |
FOR XML PATH(''), TYPE).value('.', 'varchar(max)'), 1, 2, '') | |
); | |
PRINT @myFizzBuzz; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment