How To Generate A Random Number Betweeen 1 and 10 With SQL Server

Generate Random Numbers Between A Given Number Range

This is a quick and easy script you can use right now if you need a way to generate a random number between 1 and 10 ( or another number if you adjust the number range in the script provided )

For a more indepth article and video on this check out the great Pinal Dave’s site

---- Create the variables for the random number generation

---- Set the Number Range i.e between 1 and 10
SET @Lwr = 1 ---- The lowest number of the range
SET @Upr = 10 ---- The highest number of the range
SELECT @Rnd = ROUND(((@Upr - @Lwr -1) * RAND() + @Lwr), 0)
Rob StGeorge
Senior SQL Server Database Administrator residing in Auckland, NZ

Leave a Reply

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