I'm not a DBA and haven't used TSQL or coded with recordsets since around 2004, however I thought I'd have a go at this one. In VB took less than 2 minutes, in TSQL a little more as I had to see how to use MOD in tsql.
The actual difficulty in this test (which I have to say I failed twice) isn't programming it, but ensuring the answer is exactly what was specified. From my student days I remember playing fizzbuzz as a drinking game and used the UK version, that includes the complication of fizz for numbers containing 3 (eg. 13) and buzz for numbers containing 5 (eg. 54) so I failed. I only noticed when I saw that the offered versions didn't and I double checked the requirements... I think however that all the solutions posted so far fail in that the request states:
"Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. I would therefore expext
to fail. Similarly I'd also interpret that a multiple of both fizz and buzz should only show fizzbuzz and not
15 fizz buzz fizzbuzz
Here's my code:
(on viewing other entries I think a temporary table rather than a real one is probably better solution and the insert using the identity of another table instead of a while loop interesting)
drop table dbo.fizzbuzz
CREATE TABLE [dbo].[fizzbuzz](
[num] [int] null,
[value] [varchar](15) NULL
) ON [PRIMARY]
--truncate table fizzbuzz
SET ANSI_PADDING OFF
declare @n as int
-- fill table with numbers
insert into fizzbuzz (num) values (@n)
-- find fizzbuzzes
update fizzbuzz set value='fizzbuzz'
where num % 3=0 and num % 5 = 0
-- find fizzes
update fizzbuzz set value='fizz'
where num % 3=0 --or CHARINDEX('3',cast(num as varchar(2)))>0
and value is null
-- find buzzes
update fizzbuzz set value='buzz'
where num % 5=0 --or CHARINDEX('5',cast(num as varchar(2)))>0
and value is null
-- fill in numbers
update fizzbuzz set value=num
where value is null
select value from fizzbuzz