Deprecated: explode(): Passing null to parameter #2 ($string) of type string is deprecated in /homepages/8/d352291751/htdocs/sql313/site/libraries/vendor/joomla/application/src/Web/WebClient.php on line 406
T-SQL Random Number Generator
   

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 sqlfool.com for it!)

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

http://sqlfool.com/2009/06/random-number-generator-in-tsql/

Related Articles

KILL spid (WITH STATUSONLY)

Using Transactions

Thank you for visiting!