FizzBuzz in SQL

Saw this article from Steve Jones about FizzBuzz in SQL. Figured I would try it.

Took me about 5 minutes to do. I did have a few small issues in my code at first though.

Here’s what I came up with:

declare @t table (
num varchar(10))

declare @i int

set @i = 1

while @i <= 100
insert into @t
select @i

set @i = @i+1

when convert(int,num)%3 = 0 and convert(int,num)%5 = 0 then 'FizzBuzz'
when convert(int,num)%3 = 0 then 'Fizz'
when convert(int,num)%5 = 0 then 'Buzz'
else num
from @t

The first time I did this, I made num an int because I wanted just numbers in my numbers table. This would have proven to be more difficult in the conversion of a varchar to an int. The easier way was to do the conversion on case statement.

Is this the best way to do this? No idea. BUT…it works.