Mod operator puzzle

Recently I saw a puzzle on SQL Server Central, I was intrigued to solve it myself, so here is what I was able to accomplish, and in less than 10 minutes.

DECLARE @i int
SET @i = 0

CREATE TABLE #X (val INT)

WHILE @i < 100
BEGIN
    SELECT @i = @i + 1
    INSERT INTO #X VALUES (@i)
END

SELECT val,
       val % 3,
       val % 5,
       CASE 
            WHEN val % 3 = 0 AND val % 5 = 0 THEN 'BIZZBUZZ'
            WHEN val % 3 = 0 THEN 'BIZZ'
            WHEN val % 5 = 0 THEN 'BUZZ'
       END xval
FROM   #X

DROP TABLE #X

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.