Thursday, March 22, 2018

T-SQL Random Number Generator

I recently needed to generate a random number.  I know using rand() was the way to go, but it wsnt giving me the formatting I needed.  I went online (lazy, cause I know I could format a stupid number) and found an amazing little script that I expanded upon.  It gives you a min and max range which was perfect!

Here is the code and a link to the site I got it from (giving props to Michelle at for it!)

Declare @maxRandomValue tinyint = 100
	, @minRandomValue tinyint = 0;
Select Cast(((@maxRandomValue + 1) - @minRandomValue) 
	* Rand() + @minRandomValue As tinyint) As 'randomNumber';


Powered by mod LCA