Created
February 12, 2016 04:58
-
-
Save schwartzmx/e31b724f9f7350808277 to your computer and use it in GitHub Desktop.
T-SQL FizzBuzz
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
-- Printed, while loop | |
declare @str varchar(30), @i int = 0, @max int = 100; | |
while @i <= 100 | |
begin | |
if @i % 3 = 0 | |
set @str += 'Fizz' | |
if @i % 5 = 0 | |
set @str += 'Buzz' | |
if len(@str) !> 0 | |
set @str = convert(varchar(10), @i) | |
print @str | |
set @str = '' | |
set @i +=1 | |
end | |
go | |
-- Result set | |
;with onehundred as ( | |
select top 100 row_number() over(partition by t.schema_id order by t.schema_id) as i | |
from sys.types t | |
join sys.types c | |
on t.schema_id = c.schema_id | |
) | |
select | |
case | |
when i % 3 = 0 and i % 5 = 0 then 'FizzBuzz' | |
when i % 3 = 0 then 'Fizz' | |
when i % 5 = 0 then 'Buzz' | |
else convert(varchar(10),i) | |
end as FizzBuzz | |
from onehundred | |
order by i | |
go |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment