SQL Server: Generate Random Value


The following query is to get a random value from a certain range.
For e.g. range is 0 to 10. 
DECLARE @MinValue INT
DECLARE @MaxValue INT

SET @MinValue = 0
SET @MaxValue = 10

SELECT CAST(((@MaxValue + 1) - @MinValue)
       * RAND(CHECKSUM(NEWID())) + @MinValue AS INT)
       AS RandomValue


To change range, just modify value of parameter/variable @MinValue dan @MaxValue

Click here if you like this article.


Post a Comment

0 Comments