Come utilizzare la funzione SQL RAND

Come utilizzare la funzione SQL RAND

The RAND() SQL function returns a float random number.

Many times is helpful to have an integer random number, inside a range of values, between a minumum and maximum numbers.

The following snippet shows hw to use the SQL random RAND function:

DECLARE @RdmNum float, @RdmInt int, @Max int, @Min int

-- Max value of random int
SET @Max = 15000
-- Min value of random int
SET @Min = 0

--RAND SQL function
SELECT @RdmNum = RAND()

SELECT @RdmInt = ((@Max + 1) - @Min) * @RdmNum + @Min

SELECT @RdmNum AS RandomNumber, @RdmInt AS RandomInteger

La funzione SQL RAND() restituisce un numero random float.

Spesso è utile avere un numero random che sia intero positivo e che rientri in un intervallo di valori, quindi un random tra un minimo e un massimo.

Di seguito una porzione di codice per l’utilizzo della funzione random RAND SQL:

DECLARE @RdmNum float, @RdmInt int, @Max int, @Min int

-- Max value of random int
SET @Max = 15000
-- Min value of random int
SET @Min = 0

--RAND SQL function
SELECT @RdmNum = RAND()

SELECT @RdmInt = ((@Max + 1) - @Min) * @RdmNum + @Min

SELECT @RdmNum AS RandomNumber, @RdmInt AS RandomInteger

1 commento

comments user
Maryanne

I like the valuable information you provide in your articles.
I will bookmark your blog and check again here
regularly. I’m quite certain I will learn many new stuff right here!
Good luck for the next!

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *